//========================================================================
//  SE5 AI Lists - Captain Kwok's Balance Mod v125+
//========================================================================
//
// 1. Examine each solar system we have seen
// 2. Sort our compiled lists for our priorities
//
// Balance Mod Changes:
// --------------------
// v1.29 Added   - Identification of systems with just one or two planets
//       Changed - Made adjustments to how strategic value is calculated for objects and systems; Normalized system strategic value to 0-100 scale
// v1.27 Added   - Supplemented primary patrol location with corresponding destination system and warp point location
// v1.25 Changed - Updated for various facility name/id changes
//       Added   - AI players will calculate the pollution totals for each colony
//       Added   - Strategic value of targets to attack/defense/nearby locations
// v1.21 Added   - When examining enemy ships, AI will "guess" if it has a target and compile a priority list
//       Fixed   - AIs were not always correctly marking enemy strengths specifically in their colony systems
// v1.20 Changed - Updated design type name references
//       Added   - Criteria for establishing fleet hubs
//       Added   - Fixed error in sort order for attack location strength
// v1.19 Added   - Tracking of ships to escort/protect
//       Added   - Identification of border systems
//       Changed - Improved tracking of colonies that need more units (v1.19a)
//       Changed - Added extra conditions for identifying not connected systems (v1.19c)
//       Added   - Basic facility counts (v1.19c)
//       Changed - Revised distance restrictions for colonizable planets (v1.19f)
//       Changed - Sort order for Attack locations based on distance to home system, then by weakest to strongest (v1.19h)
//       Added   - AI will track location and size of marked minefields (v1.19i)
//       Added   - AI tracking of Minesweepers for either fleet or solitary use (v1.19i)
//       Added   - AI will note locations for adding remote bases (v1.19i)
//       Changed - Revised criteria (and added priority component) for unit drop locations (v1.19i)
//       Changed - AI will sometimes consider unowned planets in potentially hostile systems as colonizable (v1.19i)
//       Fixed   - AI was not correctly counting mines in marked minefields (v1.19j)
//       Added   - Target locations will also track minimum speed of targets (v1.19j)
//       Fixed   - Explore WP indices were not being correctly sorted (v1.19j)
// v1.17 Changed - Added a distance restriction for systems to explore/scout
// v1.15 Changed - Adjusted space object strength calculation
//       Added   - Additional locations for stellar manipulation purposes
//       Added   - Identify planets that have insufficient amounts of units
//       Changed - AI will not add attack/defense locations for locations that have ships on route already
//       Changed - Improved the definition of sectors for unit drop locations
// v1.14 Changed - Space object strength calculation will consider if the object has weapons or not
// v1.13 Changed - Revised the criteria for unit drop and fleet holding locations
//       Changed - AI will cross-reference location of colonizable planets against locations of enemies
//       Changed - Added checks for determining if the AI's enemies had a presence at destination warp points
// v1.12 Added   - Mark enemy/unknown systems as systems to avoid for non-combat ships
//       Changed - Updated extra ship types to be counted as available ships for fleets
// v1.10 Added   - New Fx[Remove_Location] to delete locations
//       Changed - Modified the sort priority for defense, attack, and nearby enemy locations
// v1.09 Added   - Mark locations for fleets to hold position when they are active, but have no other orders
// v1.08 Added   - Mark warp point locations to other empires for mine/satellite laying
// v1.07 Fixed   - Error for races with Natural Merchants and marking of non-Spaceport systems
// v1.06 Changed - Calculation for addition of Defensive systems
//       Added   - Constants for adding specific location types
//       Added   - Refined conditions for marking asteroid fields for remote mining
// v1.05 Added   - Removed Space Yards from system evaluation
//       Added   - Mark plagued planets
// v1.03 Added   - Mark rioting planets from SE:V v1.25 patch
//       Added   - New variables for listing Spaceports from SE:V v1.25 patch
//       Added   - Extended system evaluation for Resupply Depots
// v0.95 Added   - New colonization changes from SE:V v1.13
// v0.92 Updated - Fx[Examine_Solar_System] so AI adds Point-Defense Ships to fleets
// v0.90 Updated - Fx[Examine_Solar_System] so AI adds extra design types to fleets
//
// Balance Mod To-do List:
// -----------------------
// - 
//
// Script Function Requests:
// -------------------------
// - Sys_Does_Space_Object_Have_Ability(sobj_id, ability_name) : boolean (Returns true if object has ability)
// - Sys_Get_Space_Object_Obscuration_Level(sobj_id) : long (Returns level of obscuration)

//------------------------------------------------------------------------
// Global Constants
//------------------------------------------------------------------------
globalconst

endglobalconst

//------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------
globalvars

endglobalvars

//------------------------------------------------------------------------
// Forward Declarations
//------------------------------------------------------------------------
deffunc

function Examine_Solar_System returns boolean
params
  sys_index:                 long
  home_sys_loc:              long
end

function Add_Location returns boolean
params
  location_type:             long
  sobj_sys:                  long
  sobj_sect:                 long
  sobj_strength:             long
  sobj_owner:                long
  sys_dist:                  long
  sobj_id:                   long
  sobj_movement:             long
  sobj_strategic_value:      long
end

function Remove_Location returns boolean
params
  location_type:             long
  index:                     long
end

enddeffunc

//------------------------------------------------------------------------
// AI_Generate_Lists
//------------------------------------------------------------------------
function AI_Generate_Lists returns boolean
vars
  home_sys_loc:              long
  home_sect_loc:             long
  sys_count:                 long
  sys_index:                 long
  sys_name:                  string
  def_sys_priority_score:    long
  atk_sys_priority_score:    long
  max_def_score:             long
  max_atk_score:             long
  strategic_value:           long
  max_strategic_value:       long
  num_items:                 long
  index:                     long
  planet_id:                 long
  planet_sys_loc:            long
  planet_sect_loc:           long
  planet_dist:               long
  planet_max_dist:           long
  planet_name:               string
  planet_type:               long
  planet_owner:              long
  planet_colonizable:        boolean
  list_count:                long
  list_index:                long
  loc_sys_name:              string
  loc_dest_sys_name:         string
  loc_sys_sect:              string
  loc_strength:              string
  loc_our_strength:          string
  loc_owner:                 string
  loc_distance:              string
  loc_ship_id:               string
  loc_ship_count:            string
  loc_strategic_value:       string
  loc_ship_move:             string
begin

  // Note our home system
  set home_sys_loc := Sys_Get_Empire_Home_System(sys_long_Player_ID)
  set home_sect_loc := Sys_Get_Empire_Home_Sector(sys_long_Player_ID)

  // Debug output
  call Sys_Debug_Print("Lists", "  - Home = " + Sys_Get_Solar_System_Name(home_sys_loc) + " System")

  // Get the number of systems in the quadrant
  set sys_count := Sys_Get_Number_Of_Solar_Systems()

  if (sys_count > 0) then
    // Initialize values for various system lists
    call lst_AI_Our_System_Colony_Count.fill(sys_count, 0)
    call lst_AI_Our_System_Population.fill(sys_count, 0)
    call lst_AI_Our_System_Facility_Count.fill(sys_count, 0)
    call lst_AI_Our_System_Resource_Facility_Count.fill(sys_count, 0)
    call lst_AI_Our_System_Strategic_Values.fill(sys_count, 0)
    call lst_AI_Our_System_Strengths.fill(sys_count, 0)
    call lst_AI_Our_System_Ship_Strengths.fill(sys_count, 0)
    call lst_AI_Our_System_Unit_Strengths.fill(sys_count, 0)
    call lst_AI_Our_System_Defense_Base_Strengths.fill(sys_count, 0)
    call lst_AI_Ally_System_Strengths.fill(sys_count, 0)
    call lst_AI_System_Value.fill(sys_count, 0.0)
    call lst_AI_Our_Colony_Systems.fill(sys_count, 0)
    call lst_AI_Our_Space_Yard_Systems.fill(sys_count, 0)
    call lst_AI_Our_Resupply_Systems.fill(sys_count, 0)
    call lst_AI_Our_Recycler_Systems.fill(sys_count, 0)
    call lst_AI_Enemy_System_Strategic_Values.fill(sys_count, 0)
    call lst_AI_Enemy_System_Strengths.fill(sys_count, 0)
    call lst_AI_Enemy_Colony_Systems.fill(sys_count, 0)
    call lst_AI_Enemy_System_Colony_Count.fill(sys_count, 0)
    call lst_AI_Enemy_System_Population.fill(sys_count, 0)
    call lst_AI_Enemy_System_Facility_Count.fill(sys_count, 0)
    call lst_AI_Enemy_System_Resource_Facility_Count.fill(sys_count, 0)
    call lst_AI_Defense_System_Total_Count.fill(sys_count, 0)
    call lst_AI_Attack_System_Total_Count.fill(sys_count, 0)
    call lst_AI_Our_System_Capital_Colonies.fill(sys_count, 0)
    call lst_AI_Our_Protected_Systems.fill(sys_count, 0)
    call lst_AI_System_Distance_To_Nearest_Colony.fill(sys_count, 0)
    call lst_AI_Enemy_Marked_Minefield_Count.fill(Sys_Get_Number_Of_Players(), 0)
    call lst_AI_System_Chokepoint_Rating.fill(sys_count, 0)
    call lst_AI_System_WP_Connections.fill(sys_count, 0)
    call lst_AI_System_WP_Hubs.fill(sys_count, 0)
    call lst_AI_System_Two_WPs.fill(sys_count, 0)
    call lst_AI_System_WP_Deadends.fill(sys_count, 0)
    call lst_AI_Attack_System_Priority_Rating.fill(sys_count, 0)
    call lst_AI_Defense_System_Priority_Rating.fill(sys_count, 0)

    // Implement better use of attack/defense system counts
    // Implement better use of our/ally/enemy strengths
    // Implement better use of system value list

    if (bool_Race_Does_Not_Use_Spaceports) then
      call lst_AI_Our_Spaceport_Systems.fill(sys_count, 1)
    else
      call lst_AI_Our_Spaceport_Systems.fill(sys_count, 0)
    endif

    // Fill our anger location lists
    set list_count := Sys_Get_Number_Of_Players()

    if (list_count > 0) then
      for list_index := 1 to list_count do
        call lst_AI_Anger_Location_Owner.add(list_index)
        call lst_AI_Anger_Location_Ship_Count.add(0)
        call lst_AI_Anger_Location_Colony_Count.add(0)
        call lst_AI_Anger_Location_Unit_Count.add(0)
      endfor
    endif

    call Set_Race_Vehicle_Amounts()

    // Examine all the systems we've seen
    for sys_index := 1 to sys_count do
      set sys_name := Sys_Get_Solar_System_Name(sys_index)
      // Load system chokepoint information from memory
      call lst_AI_System_Chokepoint_Rating.set(sys_index, Sys_Get_Event_Storage_Long(sys_index))
      call lst_AI_System_WP_Connections.fill(sys_count, Sys_Get_Solar_System_Warp_Point_Count(sys_long_Player_ID, sys_index))
      // Analyze only systems we have seen
      call Sys_Debug_Print("System", "----------------")
      call Sys_Debug_Print("System", "System Analysis:")
      if Sys_Have_Seen_System(sys_long_Player_ID, sys_index) then
        if (not bool_Are_We_Neutral_Player) or (sys_index = home_sys_loc) then
          // Examine the system
          call Examine_Solar_System(sys_index, home_sys_loc)
          // Debug Output
          call Sys_Debug_Print("System", "  - System Strength = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Strengths.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Ship Strength = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Ship_Strengths.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Base Strength = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Defense_Base_Strengths.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Unit Strength = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Unit_Strengths.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Strategic Value = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Strategic_Values.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Colony Count = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Colony_Count.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Population = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Population.get(sys_index)) + "M")
          call Sys_Debug_Print("System", "  - System Facility Count = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Facility_Count.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Resource Facility Count = " + Sys_Convert_Long_To_String(lst_AI_Our_System_Resource_Facility_Count.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Ally Strength = " + Sys_Convert_Long_To_String(lst_AI_Ally_System_Strengths.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Enemy Strength = " + Sys_Convert_Long_To_String(lst_AI_Enemy_System_Strengths.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Enemy Strategic Value = " + Sys_Convert_Long_To_String(lst_AI_Enemy_System_Strategic_Values.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Enemy Colony Count = " + Sys_Convert_Long_To_String(lst_AI_Enemy_System_Colony_Count.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Enemy Population = " + Sys_Convert_Long_To_String(lst_AI_Enemy_System_Population.get(sys_index)) + "M")
          call Sys_Debug_Print("System", "  - System Enemy Facility Count = " + Sys_Convert_Long_To_String(lst_AI_Enemy_System_Facility_Count.get(sys_index)))
          call Sys_Debug_Print("System", "  - System Enemy Resource Facility Count = " + Sys_Convert_Long_To_String(lst_AI_Enemy_System_Resource_Facility_Count.get(sys_index)))
        endif
      else
        // Debug output
        call Sys_Debug_Print("System", "  - " + sys_name + " System (" + Sys_Convert_Long_To_String(sys_index) + ") not seen")
      endif
    endfor
  endif

  // Get the maximum defense and priority score
  for sys_index := 1 to sys_count do
    // Determine maximum defense priority score
    set def_sys_priority_score := lst_AI_Defense_System_Priority_Rating.get(sys_index)
    if (def_sys_priority_score > max_def_score) then
      set max_def_score := def_sys_priority_score
    endif
    // Determine maximum attack priority score
    set atk_sys_priority_score := lst_AI_Attack_System_Priority_Rating.get(sys_index)
    if (atk_sys_priority_score > max_atk_score) then
      set max_atk_score := atk_sys_priority_score
    endif
  endfor

  // Debug output
  call Sys_Debug_Print("Lists", "------------------------")
  call Sys_Debug_Print("Lists", "System Priority Ratings:")

  // Normalize priority scores
  for sys_index := 1 to sys_count do
    if Sys_Have_Seen_System(sys_long_Player_ID, sys_index) then
      // Convert priority scores on a 100 scale
      if (max_def_score > 0) then
        set def_sys_priority_score := Sys_Divide_Long(lst_AI_Defense_System_Priority_Rating.get(sys_index) * 100, max_def_score)
        call lst_AI_Defense_System_Priority_Rating.set(sys_index, def_sys_priority_score)
      endif
      if (max_atk_score > 0) then
        set atk_sys_priority_score := Sys_Divide_Long(lst_AI_Attack_System_Priority_Rating.get(sys_index) * 100, max_atk_score)
        call lst_AI_Attack_System_Priority_Rating.set(sys_index, atk_sys_priority_score)
      endif
      // Debug Output
      call Sys_Debug_Print("Lists", "  - " + Sys_Get_Solar_System_Name(sys_index) + " System Defense Priority Rating = " + Sys_Convert_Long_To_String(lst_AI_Defense_System_Priority_Rating.get(sys_index)))
      call Sys_Debug_Print("Lists", "  - " + Sys_Get_Solar_System_Name(sys_index) + " System Attack Priority Rating = " + Sys_Convert_Long_To_String(lst_AI_Attack_System_Priority_Rating.get(sys_index)))
    endif
  endfor

  // Sort the lists for priority
  // Targeted Locations - Sorted by most targeted object
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Target_To_Defend)
  call Sys_Set_List_Sort_Field(2, lst_AI_Target_To_Defend_Dist)
  call Sys_Set_List_Sort_Field(3, lst_AI_Target_To_Defend_Count)
  call Sys_Add_List_Sort_Sort_Column(3, FALSE)
  call Sys_Execute_List_Sort()

  // Debug output - Targeted Locations
  set list_count := lst_AI_Target_To_Defend.count()
  if (list_count > 0) then
    for list_index := 1 to list_count do
      set loc_ship_id := Sys_Get_Space_Object_Name(lst_AI_Target_To_Defend.get(list_index))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Target_To_Defend_Dist.get(list_index))
      set loc_ship_count := Sys_Convert_Long_To_String(lst_AI_Target_To_Defend_Count.get(list_index))
      call Sys_Debug_Print("Lists", "-------------------")
      call Sys_Debug_Print("Lists", "Targeted Locations:")
      call Sys_Debug_Print("Lists", "  - Target Marker = " + loc_ship_ID)
      call Sys_Debug_Print("Lists", "  - Target Distance = " + loc_distance)
      call Sys_Debug_Print("Lists", "  - Target Count = " + loc_ship_count)
    endfor
  endif

  // Defense Location Priority - Strongest enemy target in a defense location
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Defense_Location_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Defense_Location_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Defense_Location_Strength)
  call Sys_Set_List_Sort_Field(4, lst_AI_Defense_Location_Assigned_Strength)
  call Sys_Set_List_Sort_Field(5, lst_AI_Defense_Location_Owner)
  call Sys_Set_List_Sort_Field(6, lst_AI_Defense_Location_System_Distance)
  call Sys_Set_List_Sort_Field(7, lst_AI_Defense_Location_Ship_ID)
  call Sys_Set_List_Sort_Field(8, lst_AI_Defense_Location_Ship_Count)
  call Sys_Set_List_Sort_Field(9, lst_AI_Defense_Location_Strategic_Value)
  call Sys_Set_List_Sort_Field(10, lst_AI_Defense_Location_Ship_Min_Move)
  call Sys_Add_List_Sort_Sort_Column(3, FALSE)
  call Sys_Execute_List_Sort()

  // Debug output - Defense Locations
  set list_count := lst_AI_Defense_Location_System.count()
  if (list_count > 0) then
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Defense_Location_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Defense_Location_Sector.get(list_index))
      set loc_strength := Sys_Convert_Long_To_String(lst_AI_Defense_Location_Strength.get(list_index))
      set loc_our_strength := Sys_Convert_Long_To_String(lst_AI_Defense_Location_Assigned_Strength.get(list_index))
      set loc_owner := Sys_Empire_Get_Empire_Name(lst_AI_Defense_Location_Owner.get(list_index))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Defense_Location_System_Distance.get(list_index))
      set loc_ship_id := Sys_Get_Space_Object_Name(lst_AI_Defense_Location_Ship_ID.get(list_index))
      set loc_ship_count := Sys_Convert_Long_To_String(lst_AI_Defense_Location_Ship_Count.get(list_index))
      set loc_strategic_value := Sys_Convert_Long_To_String(lst_AI_Defense_Location_Strategic_Value.get(list_index))
      set loc_ship_move := Sys_Convert_Long_To_String(lst_AI_Defense_Location_Ship_Min_Move.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "-----------------")
      call Sys_Debug_Print("Lists", "Defense Location:")
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " " + loc_sys_sect)
      call Sys_Debug_Print("Lists", "  - Enemy Strength = " + loc_strength)
      call Sys_Debug_Print("Lists", "  - Our Strength = " + loc_our_strength)
      call Sys_Debug_Print("Lists", "  - Sector Owner = " + loc_owner)
      call Sys_Debug_Print("Lists", "  - Distance = " + loc_distance)
      call Sys_Debug_Print("Lists", "  - Target Marker = " + loc_ship_ID)
      call Sys_Debug_Print("Lists", "  - Target Count = " + loc_ship_count)
      call Sys_Debug_Print("Lists", "  - Strategic Value = " + loc_strategic_value)
      call Sys_Debug_Print("Lists", "  - Min Move = " + loc_ship_move)
    endfor
  endif

  // Attack Location Priority - Nearest attack location, Strength
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Attack_Location_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Attack_Location_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Attack_Location_Strength)
  call Sys_Set_List_Sort_Field(4, lst_AI_Attack_Location_Assigned_Strength)
  call Sys_Set_List_Sort_Field(5, lst_AI_Attack_Location_Owner)
  call Sys_Set_List_Sort_Field(6, lst_AI_Attack_Location_System_Distance)
  call Sys_Set_List_Sort_Field(7, lst_AI_Attack_Location_Ship_ID)
  call Sys_Set_List_Sort_Field(8, lst_AI_Attack_Location_Ship_Count)
  call Sys_Set_List_Sort_Field(9, lst_AI_Attack_Location_Strategic_Value)
  call Sys_Add_List_Sort_Sort_Column(6, TRUE)
  call Sys_Add_List_Sort_Sort_Column(3, FALSE)
  call Sys_Execute_List_Sort()

  // Adjust strategic value to a 0-100 scale
  set list_count := lst_AI_Attack_Location_Strategic_Value.count()
  if (list_count > 0) then
    for list_index := 1 to list_count do
      // Pending
      set strategic_value := lst_AI_Attack_Location_Strategic_Value.get(list_index)
      if (strategic_value > max_strategic_value) then
        set max_strategic_value := strategic_value
      endif
    endfor
  endif
  // Normalize the attack location strategic value to a 0-100 scale
  if (list_count > 0) then
    for list_index := 1 to list_count do
      if (max_strategic_value > 0) then
        set strategic_value := Sys_Divide_Long(lst_AI_Attack_Location_Strategic_Value.get(list_index) * 100, max_strategic_value)
        call lst_AI_Attack_Location_Strategic_Value.set(list_index, strategic_value)
      else
        call lst_AI_Attack_Location_Strategic_Value.set(list_index, 0)
      endif
    endfor
  endif

  // Debug output - Attack Locations
  set list_count := lst_AI_Attack_Location_System.count()
  if (list_count > 0) then
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Attack_Location_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Attack_Location_Sector.get(list_index))
      set loc_strength := Sys_Convert_Long_To_String(lst_AI_Attack_Location_Strength.get(list_index))
      set loc_our_strength := Sys_Convert_Long_To_String(lst_AI_Attack_Location_Assigned_Strength.get(list_index))
      set loc_owner := Sys_Empire_Get_Empire_Name(lst_AI_Attack_Location_Owner.get(list_index))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Attack_Location_System_Distance.get(list_index))
      set loc_ship_id := Sys_Get_Space_Object_Name(lst_AI_Attack_Location_Ship_ID.get(list_index))
      set loc_ship_count := Sys_Convert_Long_To_String(lst_AI_Attack_Location_Ship_Count.get(list_index))
      set loc_strategic_value := Sys_Convert_Long_To_String(lst_AI_Attack_Location_Strategic_Value.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "----------------")
      call Sys_Debug_Print("Lists", "Attack Location:")
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " " + loc_sys_sect)
      call Sys_Debug_Print("Lists", "  - Enemy Strength = " + loc_strength)
      call Sys_Debug_Print("Lists", "  - Our Strength = " + loc_our_strength)
      call Sys_Debug_Print("Lists", "  - Sector Owner = " + loc_owner)
      call Sys_Debug_Print("Lists", "  - Distance = " + loc_distance)
      call Sys_Debug_Print("Lists", "  - Target Marker = " + loc_ship_ID)
      call Sys_Debug_Print("Lists", "  - Target Count = " + loc_ship_count)
      call Sys_Debug_Print("Lists", "  - Strategic Value = " + loc_strategic_value)
    endfor
  endif

  // Nearby Enemy Location Priority - Strongest enemy target in a nearby enemy location
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Nearby_Enemy_Location_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Nearby_Enemy_Location_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Nearby_Enemy_Location_Strength)
  call Sys_Set_List_Sort_Field(4, lst_AI_Nearby_Enemy_Location_Assigned_Strength)
  call Sys_Set_List_Sort_Field(5, lst_AI_Nearby_Enemy_Location_Owner)
  call Sys_Set_List_Sort_Field(6, lst_AI_Nearby_Enemy_Location_System_Distance)
  call Sys_Set_List_Sort_Field(7, lst_AI_Nearby_Enemy_Location_Ship_ID)
  call Sys_Set_List_Sort_Field(8, lst_AI_Nearby_Enemy_Location_Ship_Count)
  call Sys_Set_List_Sort_Field(9, lst_AI_Nearby_Enemy_Location_Strategic_Value)
  call Sys_Set_List_Sort_Field(10, lst_AI_Nearby_Enemy_Location_Ship_Min_Move)
  call Sys_Add_List_Sort_Sort_Column(3, FALSE)
  call Sys_Execute_List_Sort()

  // Debug output - Nearby Enemy Locations
  set list_count := lst_AI_Nearby_Enemy_Location_System.count()
  if (list_count > 0) then
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Nearby_Enemy_Location_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Nearby_Enemy_Location_Sector.get(list_index))
      set loc_strength := Sys_Convert_Long_To_String(lst_AI_Nearby_Enemy_Location_Strength.get(list_index))
      set loc_our_strength := Sys_Convert_Long_To_String(lst_AI_Nearby_Enemy_Location_Assigned_Strength.get(list_index))
      set loc_owner := Sys_Empire_Get_Empire_Name(lst_AI_Nearby_Enemy_Location_Owner.get(list_index))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Nearby_Enemy_Location_System_Distance.get(list_index))
      set loc_ship_id := Sys_Get_Space_Object_Name(lst_AI_Nearby_Enemy_Location_Ship_ID.get(list_index))
      set loc_ship_count := Sys_Convert_Long_To_String(lst_AI_Nearby_Enemy_Location_Ship_Count.get(list_index))
      set loc_strategic_value := Sys_Convert_Long_To_String(lst_AI_Nearby_Enemy_Location_Strategic_Value.get(list_index))
      set loc_ship_move := Sys_Convert_Long_To_String(lst_AI_Nearby_Enemy_Location_Ship_Min_Move.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "----------------------")
      call Sys_Debug_Print("Lists", "Nearby Enemy Location:")
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " " + loc_sys_sect)
      call Sys_Debug_Print("Lists", "  - Enemy Strength = " + loc_strength)
      call Sys_Debug_Print("Lists", "  - Our Strength = " + loc_our_strength)
      call Sys_Debug_Print("Lists", "  - Sector Owner = " + loc_owner)
      call Sys_Debug_Print("Lists", "  - Distance = " + loc_distance)
      call Sys_Debug_Print("Lists", "  - Target Marker = " + loc_ship_ID)
      call Sys_Debug_Print("Lists", "  - Target Count = " + loc_ship_count)
      call Sys_Debug_Print("Lists", "  - Strategic Value = " + loc_strategic_value)
      call Sys_Debug_Print("Lists", "  - Min Move = " + loc_ship_move)
    endfor
  endif

  // Anger locations
  set list_count := Sys_Get_Number_Of_Players()

  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "----------------------")
    call Sys_Debug_Print("Lists", "Anger Location Counts:")

    for list_index := 1 to list_count do
      if (list_index <> sys_long_Player_ID) then
        call Sys_Debug_Print("Lists", " - " + Sys_Empire_Get_Empire_Name(list_index))
        call Sys_Debug_Print("Lists", "    - Colonies (" + Sys_Convert_Long_To_String(lst_AI_Anger_Location_Colony_Count.get(list_index)) + "); Ships (" + Sys_Convert_Long_To_String(lst_AI_Anger_Location_Ship_Count.get(list_index)) + "); Unit Groups (" + Sys_Convert_Long_To_String(lst_AI_Anger_Location_Unit_Count.get(list_index)) + ")")
      endif
    endfor
  endif

  // Repair Location Priority - Sort by most tonnage to be repair in a repair location
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Repair_Location_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Repair_Location_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Repair_Location_Repair_Tonnage)
  call Sys_Add_List_Sort_Sort_Column(3, FALSE)
  call Sys_Execute_List_Sort()

  // Debug output - Repair Locations
  set list_count := lst_AI_Repair_Location_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "----------------")
    call Sys_Debug_Print("Lists", "Repair Location:")

    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Repair_Location_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Repair_Location_Sector.get(list_index))
      set loc_strength := Sys_Convert_Long_To_String(lst_AI_Repair_Location_Repair_Tonnage.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " " + loc_sys_sect + "; Repair = " + loc_strength + "kT")
    endfor
  endif

  // Survey Priority - Nearest to our home system
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Survey_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Survey_Distance_To_Home)
  call Sys_Add_List_Sort_Sort_Column(2, TRUE)
  call Sys_Execute_List_Sort()

  // Debug output - Survey Systems
  set list_count := lst_AI_Survey_System.count()
  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "---------------")
    call Sys_Debug_Print("Lists", "Survey Systems:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Survey_System.get(list_index))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Survey_Distance_To_Home.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System (" + loc_distance + " sys)")
    endfor
  endif

  // Explore Priority - Nearest to our home system
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Explore_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Explore_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Explore_WP_ID)
  call Sys_Set_List_Sort_Field(4, lst_AI_Explore_WP_Index)
  call Sys_Set_List_Sort_Field(5, lst_AI_Explore_Distance_To_Home)
  call Sys_Add_List_Sort_Sort_Column(5, TRUE)
  call Sys_Execute_List_Sort()

  // Debug output - Explore Systems
  set list_count := lst_AI_Explore_System.count()
  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "-----------------------")
    call Sys_Debug_Print("Lists", "Warp Points to Explore:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Explore_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Explore_Sector.get(list_index))
      set loc_dest_sys_name := Sys_Get_Solar_System_Name(Sys_Get_Solar_System_Warp_Point_Dest_System(sys_long_Player_ID, lst_AI_Explore_System.get(list_index), lst_AI_Explore_WP_Index.get(list_index)))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Explore_Distance_To_Home.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System " + loc_sys_sect + " (" + loc_distance + " sys)" + "; WP Dest = " + loc_dest_sys_name + " System")
    endfor
  endif

  // Unit Drop Location Priority
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Unit_Drop_Location_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Unit_Drop_Location_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Unit_Drop_Location_Dest_Sys)
  call Sys_Set_List_Sort_Field(4, lst_AI_Unit_Drop_Location_Priority)
  call Sys_Add_List_Sort_Sort_Column(4, FALSE)
  call Sys_Execute_List_Sort()

  // Debug output - Unit Drop Locations
  set list_count := lst_AI_Unit_Drop_Location_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "--------------------")
    call Sys_Debug_Print("Lists", "Unit Drop Locations:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Unit_Drop_Location_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Unit_Drop_Location_Sector.get(list_index))
      set loc_strength := Sys_Convert_Long_To_String(lst_AI_Unit_Drop_Location_Priority.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System " + loc_sys_sect + "; Priority = " + loc_strength)
    endfor
  endif

  // Remote Base Location Priority
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Remote_Base_Location_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Remote_Base_Location_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Remote_Base_Location_Priority)
  call Sys_Add_List_Sort_Sort_Column(3, FALSE)
  call Sys_Execute_List_Sort()

  // Debug output - Remote Base Locations
  set list_count := lst_AI_Remote_Base_Location_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "----------------------")
    call Sys_Debug_Print("Lists", "Remote Base Locations:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Remote_Base_Location_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Remote_Base_Location_Sector.get(list_index))
      set loc_strength := Sys_Convert_Long_To_String(lst_AI_Remote_Base_Location_Priority.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System " + loc_sys_sect + "; Priority = " + loc_strength)
    endfor
  endif

  // Debug output - Space Yard hubs
  set list_count := lst_AI_Our_Space_Yard_Hubs.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "----------------")
    call Sys_Debug_Print("Lists", "Space Yard Hubs:")
    for list_index := 1 to list_count do
      set loc_ship_id := Sys_Get_Space_Object_Name(lst_AI_Our_Space_Yard_Hubs.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_ship_id)
    endfor
  endif

  // Debug output - Disabled Ships
  set list_count := lst_AI_Our_Disabled_Ships.count()

  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "---------------")
    call Sys_Debug_Print("Lists", "Disabled Ships:")
    for list_index := 1 to list_count do
      set loc_ship_id := Sys_Get_Space_Object_Name(lst_AI_Our_Disabled_Ships.get(list_index))
      set loc_sys_name := Sys_Get_Solar_System_Name(Sys_Get_Space_Object_System_Location(lst_AI_Our_Disabled_Ships.get(list_index)))
      set loc_sys_sect := Convert_Sector_To_String(Sys_Get_Space_Object_Sector_Location(lst_AI_Our_Disabled_Ships.get(list_index)))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_ship_id + " in " + loc_sys_name + " System " + loc_sys_sect)
    endfor
  endif

  // Fleet Holding Location Priority
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Fleet_Holding_System)
  call Sys_Set_List_Sort_Field(2, lst_AI_Fleet_Holding_Sector)
  call Sys_Set_List_Sort_Field(3, lst_AI_Fleet_Holding_Dest_System)
  call Sys_Set_List_Sort_Field(4, lst_AI_Fleet_Holding_Location_Priority)
  call Sys_Add_List_Sort_Sort_Column(4, FALSE)
  call Sys_Execute_List_Sort()

  // Debug output - Fleet Holding Locations
  set list_count := lst_AI_Fleet_Holding_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "------------------------")
    call Sys_Debug_Print("Lists", "Fleet Holding Locations:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Fleet_Holding_System.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Fleet_Holding_Sector.get(list_index))
      set loc_dest_sys_name := Sys_Get_Solar_System_Name(lst_AI_Fleet_Holding_Dest_System.get(list_index))
      set loc_strategic_value := Sys_Convert_Long_To_String(lst_AI_Fleet_Holding_Location_Priority.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System " + loc_sys_sect)
      call Sys_Debug_Print("Lists", "    - Warp to " + loc_dest_sys_name)
      call Sys_Debug_Print("Lists", "  - Priority Level = " + loc_strategic_value)
    endfor
  endif

  // Debug output - Border Systems
  set list_count := lst_AI_Border_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "---------------")
    call Sys_Debug_Print("Lists", "Border Systems:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Border_System.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System")
    endfor
  endif

  // Debug output - Patrol Systems
  set list_count := lst_AI_Patrol_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "---------------")
    call Sys_Debug_Print("Lists", "Patrol Systems:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Patrol_System.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System")
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Patrol_Dest_System.get(list_index))
      call Sys_Debug_Print("Lists", "    - Destination system = " + loc_sys_name + " System")
    endfor
  endif

  // Debug output - Secondary Patrol Systems
  set list_count := lst_AI_Patrol_Secondary_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "-------------------------")
    call Sys_Debug_Print("Lists", "Secondary Patrol Systems:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Patrol_Secondary_System.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System")
    endfor
  endif

  // Debug output - Hidden Enemy Systems
  set list_count := lst_AI_Hidden_Enemy_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "---------------------")
    call Sys_Debug_Print("Lists", "Hidden Enemy Systems:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Hidden_Enemy_System.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System")
    endfor
  endif

  // Debug output - Hidden Other Systems
  set list_count := lst_AI_Hidden_Other_System.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "---------------------")
    call Sys_Debug_Print("Lists", "Hidden Other Systems:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Hidden_Other_System.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System")
    endfor
  endif

  // Debug output - Shared Systems
  set list_count := lst_AI_Mutual_Colony_Systems.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "---------------")
    call Sys_Debug_Print("Lists", "Mutual Systems:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Mutual_Colony_Systems.get(list_index))
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System")
    endfor
  endif

  // Debug output - Systems To Avoid
  set list_count := lst_AI_System_To_Avoid.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "-----------------")
    call Sys_Debug_Print("Lists", "Systems To Avoid:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_System_To_Avoid.get(list_index))
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System")
    endfor
  endif

  // Note our marked minefields
  call Sys_Get_Marked_Minefield_Locations(sys_long_Player_ID, lst_AI_Marked_Minefield_Sys, lst_AI_Marked_Minefield_Sect)

  set list_count := lst_AI_Marked_Minefield_Sys.count()

  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "------------------")
    call Sys_Debug_Print("Lists", "Marked Minefields:")
    // Determine the size of each marked minefield
    for list_index := 1 to list_count do
      call lst_AI_Marked_Minefield_Count.add(Get_Mine_Group_Count(lst_AI_Marked_Minefield_Sys.get(list_index), lst_AI_Marked_Minefield_Sect.get(list_index)))
      set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Marked_Minefield_Sys.get(list_index))
      set loc_sys_sect := Convert_Sector_To_String(lst_AI_Marked_Minefield_Sect.get(list_index))
      set loc_ship_count := Sys_Convert_Long_To_String(lst_AI_Marked_Minefield_Count.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System " + loc_sys_sect + " (" + loc_ship_count + "x)")
      // Unmarked minefields with no count
      if (loc_ship_count = 0) then
        if Sys_Give_Space_Object_Order_Any(0, ORDER_TOGGLE_MARKED_MINEFIELD, lst_AI_Marked_Minefield_Sys.get(list_index), lst_AI_Marked_Minefield_Sect.get(list_index), 0) then
          // Debug output
          call Sys_Debug_Print("Lists", "    - Removed")
        endif
      endif
    endfor
  endif

  // Note how many total mines our enemies have planted that can hurt us
  set list_count := Sys_Get_Number_Of_Players()

  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "-------------------")
    call Sys_Debug_Print("Lists", "Total Marked Mines:")
    // Report the total number of mines that belong to each player
    for list_index := 1 to list_count do
      if (list_index <> sys_long_Player_ID) then
        // Debug output
        call Sys_Debug_Print("Lists", "  - " + Sys_Empire_Get_Empire_Name(list_index) + " = " + Sys_Convert_Long_To_String(lst_AI_Enemy_Marked_Minefield_Count.get(list_index)))
      endif
    endfor
  endif

  // Resource Asteroids Priority - Nearest to our home system
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Asteroid_Fields_For_Resources)
  call Sys_Set_List_Sort_Field(2, lst_AI_Asteroid_Fields_Dist_For_Resources)
  call Sys_Add_List_Sort_Sort_Column(2, TRUE)
  call Sys_Execute_List_Sort()

  // Debug output - Asteroid Fields
  set list_count := lst_AI_Asteroid_Fields_For_Resources.count()
  if (list_count > 0) then
    call Sys_Debug_Print("Lists", "------------------------------")
    call Sys_Debug_Print("Lists", "Asteroid Fields for Resources:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(Sys_Get_Space_Object_System_Location(lst_AI_Asteroid_Fields_For_Resources.get(list_index)))
      set loc_sys_sect := Convert_Sector_To_String(Sys_Get_Space_Object_Sector_Location(lst_AI_Asteroid_Fields_For_Resources.get(list_index)))
      call Sys_Debug_Print("Lists", "  - " + loc_sys_name + " System " + loc_sys_sect)
    endfor
  endif

  // Planet Priority
  call Sys_Prepare_For_Planet_Sort()
  call Sys_Add_Planet_Sort_Column(SORT_PLANET_BY_ENEMIES_PRESENT_IN_SYSTEM, TRUE)
  call Sys_Add_Planet_Sort_Column(SORT_PLANET_BY_SYSTEM_DISTANCE_TO_HOME_SYSTEM, TRUE)
  call Sys_Add_Planet_Sort_Column(SORT_PLANET_BY_HAS_GRANTED_TECH, FALSE)
  call Sys_Add_Planet_Sort_Column(SORT_PLANET_BY_BREATHABLE, FALSE)
  call Sys_Add_Planet_Sort_Column(SORT_PLANET_BY_PLANET_SIZE, FALSE)
  call Sys_Add_Planet_Sort_Column(SORT_PLANET_BY_TOTAL_PLANET_VALUE, FALSE)
  call Sys_Execute_Planet_Sort(sys_long_Player_ID, lst_AI_Colonizable_Planets)

  call lst_AI_Planets_To_Colonize_Num_Types.fill(10, 0)

  // Generate list of colonizable planets
  set num_items := lst_AI_Colonizable_Planets.count()

  if (num_items > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "--------------------")
    call Sys_Debug_Print("Lists", "Planets to Colonize:")
    // Determine which planets are actually colonizable
    for index := 1 to num_items do
      set planet_id := lst_AI_Colonizable_Planets.get(index)
      set planet_name := Sys_Get_Space_Object_Name(planet_id)
      set planet_sys_loc := Sys_Get_Space_Object_System_Location(planet_id)
      set planet_sect_loc := Sys_Get_Space_Object_Sector_Location(planet_id)
      set planet_max_dist := 50
      set planet_type := Sys_Get_Planet_Physical_Type(planet_id)
      set planet_owner := Sys_Get_Space_Object_Owner(planet_id)
      set planet_colonizable := TRUE
      // Distance factors
      if Sys_Is_Planet_Breathable(planet_id, sys_long_Player_ID) then
        set planet_max_dist := planet_max_dist + 30
      endif

      // Don't considered planets already colonized
      if (planet_owner > 0) then
        set planet_colonizable := FALSE
      endif
      // Don't consider planets with colony ships in transit
      if (Sys_Get_Number_Of_Ships_With_Order(sys_long_Player_ID, ORDER_COLONIZE, 0, 0, planet_id) > 0) then
        set planet_colonizable := FALSE
      endif
      // Don't consider planets too far from our other colonies
      if (not Do_We_Have_A_Colony_Within_Distance(planet_sys_loc, planet_sect_loc, planet_max_dist)) then
        set planet_colonizable := FALSE
      endif
      // Don't consider planets in our systems to avoid
      if (lst_AI_System_To_Avoid.indexof(planet_sys_loc) > 0) then
        set planet_colonizable := FALSE
      endif
      // Don't consider planets in systems we are weaker than our enemy
      if (lst_AI_Our_System_Strengths.get(planet_sys_loc) < lst_AI_Enemy_System_Strengths.get(planet_sys_loc)) then
        set planet_colonizable := FALSE
      endif
      // Don't consider planets with an enemy present in the same sector
      if Is_Enemy_Present_In_Sector(planet_sys_loc, planet_sect_loc) then
        set planet_colonizable := FALSE
      endif
      // Don't consider planets in systems with marked minefields
      if (lst_AI_Marked_Minefield_Sys.indexof(planet_sys_loc) > 0) then
        if (lst_AI_Marked_Minefield_Count.get(lst_AI_Marked_Minefield_Sys.indexof(planet_sys_loc))) then
          set planet_colonizable := FALSE
        endif
      endif
      // If colonizable, then add to our list of planets to colonize
      if (planet_colonizable) then
        call lst_AI_Planets_To_Colonize_Num_Types.set(planet_type, lst_AI_Planets_To_Colonize_Num_Types.get(planet_type) + 1)
        call lst_AI_Planets_To_Colonize.add(planet_id)
        // Debug output
        call Sys_Debug_Print("Lists", "  - " + planet_name + " (" + Sys_Convert_Long_To_String(planet_dist) + ")")
      endif
    endfor
  endif

  // Storms to Destroy - Sort by nearest to home system
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Storms_To_Destroy)
  call Sys_Set_List_Sort_Field(2, lst_AI_Storms_To_Destroy_Distance)
  call Sys_Add_List_Sort_Sort_Column(2, TRUE)
  call Sys_Execute_List_Sort()

  // Debug output - Storms to Destroy
  set list_count := lst_AI_Storms_To_Destroy.count()
  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "------------------")
    call Sys_Debug_Print("Lists", "Storms to Destroy:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(Sys_Get_Space_Object_System_Location(lst_AI_Storms_To_Destroy.get(list_index)))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Storms_To_Destroy_Distance.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - Storm in " + loc_sys_name + " System (" + loc_distance + " sys)")
    endfor
  endif

  // Planets to Destroy - Sort by nearest to home system
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Planets_To_Destroy)
  call Sys_Set_List_Sort_Field(2, lst_AI_Planets_To_Destroy_Distance)
  call Sys_Add_List_Sort_Sort_Column(2, TRUE)
  call Sys_Execute_List_Sort()

  // Debug output - Planets to Destroy
  set list_count := lst_AI_Planets_To_Destroy.count()
  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "-------------------")
    call Sys_Debug_Print("Lists", "Planets to Destroy:")
    for list_index := 1 to list_count do
      set planet_name := Sys_Get_Space_Object_Name(lst_AI_Planets_To_Destroy.get(list_index))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Planets_To_Destroy_Distance.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - " + planet_name + " (" + loc_distance + " sys)")
    endfor
  endif

  // Stars to Destroy - Sort by nearest to home system
  call Sys_Prepare_For_List_Sort()
  call Sys_Set_List_Sort_Field(1, lst_AI_Stars_To_Destroy)
  call Sys_Set_List_Sort_Field(2, lst_AI_Stars_To_Destroy_Distance)
  call Sys_Add_List_Sort_Sort_Column(2, TRUE)
  call Sys_Execute_List_Sort()

  // Debug output - Storms to Destroy
  set list_count := lst_AI_Stars_To_Destroy.count()
  if (list_count > 0) then
    // Debug output
    call Sys_Debug_Print("Lists", "-----------------")
    call Sys_Debug_Print("Lists", "Stars to Destroy:")
    for list_index := 1 to list_count do
      set loc_sys_name := Sys_Get_Solar_System_Name(Sys_Get_Space_Object_System_Location(lst_AI_Stars_To_Destroy.get(list_index)))
      set loc_distance := Sys_Convert_Long_To_String(lst_AI_Stars_To_Destroy_Distance.get(list_index))
      // Debug output
      call Sys_Debug_Print("Lists", "  - Star in " + loc_sys_name + " System (" + loc_distance + " sys)")
    endfor
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Examine_Solar_System
//------------------------------------------------------------------------
function Examine_Solar_System returns boolean
params
  sys_index:                 long
  home_sys_loc:              long
vars
  sys_name:                  string
  sys_has_visible_enemy:     boolean
  sys_has_visible_other:     boolean
  sys_planet_count:          long
  sys_strategic_value:       long
  dest_sys:                  long
  dest_sys_enemy:            boolean
  dest_sys_wp_enemy:         boolean
  dest_sys_no_colonies:      boolean
  dest_sys_empty:            boolean
  wp_index:                  long
  wp_count:                  long
  wp_sect:                   long
  wp_id:                     long
  sobj_list:                 longlist
  sobj_index:                long
  sobj_count:                long
  sobj_id:                   long
  sobj_owner:                long
  sobj_type:                 long
  sobj_sect:                 long
  sobj_strength:             long
  sobj_strategic_value:      long
  sobj_movement:             long
  sobj_tonnage:              long
  sobj_dmg_tonnage:          long
  sobj_dmg_pct:              long
  sobj_max_cargo_space:      long
  sobj_facility_space:       long
  sobj_used_cargo_space:     long
  total_enemy_armor:         long
  total_friend_armor:        long
  total_enemy_shields:       long
  total_friend_shields:      long
  sys_dist:                  long
  sys_dist_to_colony:        long
  sys_strength:              long
  sys_capital:               long
  plr_has_colony:            boolean
  is_core_colony:            boolean
  is_colony_to_defend:       boolean
  is_star_present:           boolean
  is_border_system:          boolean
  is_enemy_present:          boolean
  are_others_present:        boolean
  has_spaceport:             boolean
  has_resupply_depot:        boolean
  has_space_yard:            boolean
  has_comp_factory:          boolean
  has_sys_robotoid:          boolean
  has_resource_converter:    boolean
  has_recycler:              boolean
  has_ship_training:         boolean
  has_fleet_training:        boolean
  has_combat_mod:            boolean
  has_damage_mod:            boolean
  has_dimensional_rift:      boolean
  res_planet_value:          resources
  total_planet_val:          real
  ast_value:                 resources
  ast_sys_loc:               long
  ast_sect_loc:              long
  ast_minerals:              real
  ast_organics:              real
  ast_radioactives:          real
  ships_in_transit:          long
  ai_design_type:            string
  planet_id:                 long
  planet_name:               string
  pollution_pts:             long
  lst_facility_id:           longlist
  lst_facility_level:        longlist
  facility_count:            long
  facility_index:            long
  facility_id:               long
  facility_name:             string
  this_strength:             long
  max_strength:              long
  max_strategic_value:       long
  max_facilities:            long
  num_facilities:            long
  num_minerals:              long
  num_organics:              long
  num_radioactives:          long
  num_monoliths:             long
  num_research:              long
  num_intel:                 long
  num_population:            long
  our_strength:              long
  enemy_strength:            long
  list_index:                long
  target_sobj_id:            long
  target_sobj_dist:          long
  target_sys_loc:            long
  target_sect_loc:           long
  target_index:              long
  target_count:              long
  def_sys_priority_score:    long
  atk_sys_priority_score:    long
  loc_priority:              long
  choke_wp_ratio:            long
begin

  // Get the system's details
  set plr_has_colony := Sys_Player_Has_Colony_In_System(sys_long_Player_ID, sys_index)
  set sys_dist := Sys_Get_System_Distance_Between_Solar_Systems(sys_index, home_sys_loc)
  set sys_dist_to_colony := Get_Distance_To_Nearest_Colony(sys_index, 100100)
  set sys_strength := 0
  set this_strength := 0
  set max_strength := -999999
  set max_strategic_value := -999999
  set sys_capital := 0
  set sys_name := Sys_Get_Solar_System_Name(sys_index)
  set is_star_present := FALSE
  set is_border_system := FALSE
  set is_enemy_present := Is_Enemy_Present_In_System(sys_index)
  set are_others_present := Are_Others_Present_In_System(sys_index)
  set sys_has_visible_enemy := FALSE
  set sys_has_visible_other := FALSE

  // Test
  if (lst_AI_System_WP_Connections.get(sys_index) > 0) then
    set choke_wp_ratio := Sys_Divide_Long(lst_AI_System_Chokepoint_Rating.get(sys_index) * 100, lst_AI_System_WP_Connections.get(sys_index))
  endif

  // Debug output
  call Sys_Debug_Print("System", "  - " + sys_name + " System" + " (" +  + Sys_Convert_Long_To_String(sys_index) + "); System Distance = " + Sys_Convert_Long_To_String(sys_dist))
  call Sys_Debug_Print("System", "    - WP Connections = " + Sys_Convert_Long_To_String(lst_AI_System_WP_Connections.get(sys_index)))
  call Sys_Debug_Print("System", "    - Chokepoint Rating = " + Sys_Convert_Long_To_String(lst_AI_System_Chokepoint_Rating.get(sys_index)))
  call Sys_Debug_Print("System", "    - Chokepoint/WP Ratio = " + Sys_Convert_Long_To_String(choke_wp_ratio))
  call Sys_Debug_Print("System", "    - Objects:")

  // Save the approximate distance to the next colony
  call lst_AI_System_Distance_To_Nearest_Colony.set(sys_index, sys_dist_to_colony)

  // Examine all the visible objects we can see in this system
  call Sys_Get_All_Visible_Space_Objects_In_System(sys_long_Player_ID, sys_index, sobj_list)
  set sobj_count := sobj_list.count()

  if (sobj_count > 0) then
    for sobj_index := 1 to sobj_count do

      // Get the space object's details
      set sobj_id := sobj_list.get(sobj_index)
      set sobj_owner := Sys_Get_Space_Object_Owner(sobj_id)
      set sobj_type := Sys_Get_Space_Object_Type(sobj_id)
      set sobj_sect := Sys_Get_Space_Object_Sector_Location(sobj_id)
      set sobj_strength := Calculate_Space_Object_Strength(sobj_id)
      // Reduce the object's strength if it doesn't have any weapons
      if (Sys_Get_Space_Object_Total_Weapon_Damage(sobj_id) = 0) then
        set sobj_strength := Sys_Divide_Long(sobj_strength, 5)
      endif
      // Reset strategic value
      set sobj_strategic_value := 0

      if (sobj_type = SPACE_OBJECT_TYPE_SHIP) then
        set sobj_movement := Sys_Get_Space_Object_Maximum_Movement(sobj_id)
      endif

      // Note stars
      if (sobj_type = SPACE_OBJECT_TYPE_STAR) then
        set is_star_present := TRUE
      endif

      // Asteroid Fields
      if (sobj_type = SPACE_OBJECT_TYPE_ASTEROIDS) then
        // Only add asteroid fields near our colonies without enemies present
        if (not Does_Enemy_Have_Colony_In_System(sys_index)) then
          // Is the asteroid field close to a colony?
          if (sys_dist_to_colony < 40) then
            set ast_value := Sys_Get_Space_Object_Planet_Value(sobj_id)
            set ast_minerals := ast_value.get(RESOURCE_TYPE_MINERALS)
            set ast_organics := ast_Value.get(RESOURCE_TYPE_ORGANICS)
            set ast_radioactives := ast_value.get(RESOURCE_TYPE_RADIOACTIVES)
            // Note asteroid fields for remote resource gathering
            if (ast_minerals >= 150) or (ast_organics >= 150) or (ast_radioactives >= 150) then
              call lst_AI_Asteroid_Fields_For_Resources.add(sobj_id)
              call lst_AI_Asteroid_Fields_Dist_For_Resources.add(sys_dist)
            endif
          endif
          // Add to list of asteroid fields for planet creation
          if Sys_Player_Has_Colony_In_System(sys_long_Player_ID, sys_index) then
            call lst_AI_Asteroid_Fields_For_Planets.add(sobj_id)
          endif
        endif
      endif

      // Note storms in our systems
      if (sobj_type = SPACE_OBJECT_TYPE_STORM) then
        if (plr_has_colony) then
          call lst_AI_Storms_In_Our_Systems.add(sobj_id)
          // Track storms to destroy
          call lst_AI_Storms_To_Destroy.add(sobj_id)
          call lst_AI_Storms_To_Destroy_Distance.add(sys_dist)
        endif
      endif

      // Note Planets and their value
      if (sobj_type = SPACE_OBJECT_TYPE_PLANET) then
        set planet_name := Sys_Get_Space_Object_Name(sobj_id)
        set sys_planet_count := sys_planet_count + 1
        // Reset variables
        set has_spaceport := FALSE
        set has_resupply_depot := FALSE
        set has_space_yard := FALSE
        set has_comp_factory := FALSE
        set has_sys_robotoid := FALSE
        set has_resource_converter := FALSE
        set has_recycler := FALSE
        set has_ship_training := FALSE
        set has_fleet_training := FALSE
        set has_combat_mod := FALSE
        set has_damage_mod := FALSE
        set has_dimensional_rift := FALSE
        set num_minerals := 0
        set num_organics := 0
        set num_radioactives := 0
        set num_monoliths := 0
        set num_research := 0
        set num_intel := 0

        // Debug output
        call Sys_Debug_Print("System", "      - " + planet_name + " " + Convert_Sector_To_String(sobj_sect))
        call Sys_Debug_Print("System", "        - Sobj ID = " + Sys_Convert_Long_To_String(sobj_id))

        // Can we colonize the planet?
        if Sys_Can_Colonize_Planet(sys_long_Player_ID, sobj_id) then
          set res_planet_value := Sys_Get_Space_Object_Planet_Value(sobj_id)
          set total_planet_val := res_planet_value.get(1) + res_planet_value.get(2) + res_planet_value.get(3)
          call lst_AI_System_Value.set(sys_index, lst_AI_System_Value.get(sys_index) + total_planet_val)
          // Is it available for colonization?
          if (sobj_owner = 0) then
            call lst_AI_Colonizable_Planets.add(sobj_id)
          endif
        endif

        // If the colony is owned...
        if (sobj_owner > 0) then
          // Add extra strategic value if this is a player's homeworld
          if (sobj_owner <> sys_long_Player_ID) then
            if (Sys_Get_Empire_Home_System(sobj_owner) = sys_index) then
              if (Sys_Get_Empire_Home_Sector(sobj_owner) = Sys_Get_Space_Object_Sector_Location(sobj_id)) then
                set sobj_strategic_value := sobj_strategic_value + 100
              endif
            endif
          endif
          // Basic details
          set num_population := Sys_Get_Planet_Population(sobj_id)
          set sobj_strategic_value := sobj_strategic_value + Sys_Divide_Long(num_population, 100)
          set num_facilities := Get_Total_Number_Of_Facilities_On_Planet(sobj_id)
          // Get a list of the colony's facilities
          call Sys_Get_Space_Object_Cargo_List_Of_Facilities(sobj_id, lst_facility_id, lst_facility_level)
          set facility_count := lst_facility_id.count()
          // Examine the colony's facilities
          if (facility_count > 0) then
            for facility_index := 1 to facility_count do
              set facility_id := lst_facility_id.get(facility_index)
              set facility_name := Sys_Get_Facility_Name(facility_id)
              // Examine the facilities present and computer their strategic value
              case facility_name
                "Space Port":
                  set has_spaceport := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 50
                "Space Yard Facility":
                  set has_space_yard := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 25
                "Resupply Depot":
                  set has_resupply_depot := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 25
                "Research Center":
                  set num_research := num_research + 1
                  set sobj_strategic_value := sobj_strategic_value + 2
                "Intelligence Center":
                  set num_intel := num_intel + 1
                  set sobj_strategic_value := sobj_strategic_value + 1
                "Mineral Miner Facility":
                  set num_minerals := num_minerals + 1
                  set sobj_strategic_value := sobj_strategic_value + 3
                "Organics Farm Facility":
                  set num_organics := num_organics + 1
                  set sobj_strategic_value := sobj_strategic_value + 1
                "Radioactives Extraction Facility":
                  set num_radioactives := num_radioactives + 1
                  set sobj_strategic_value := sobj_strategic_value + 2
                "Monolith Facility":
                  set num_monoliths := num_monoliths + 1
                  set sobj_strategic_value := sobj_strategic_value + 3
                "Components Factory":
                  set sobj_strategic_value := sobj_strategic_value + 10
                "System Robotoid Factory":
                  set has_sys_robotoid := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 25
                "Resource Converter":
                  set has_resource_converter := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 10
                "Ultra-Recycler":
                  set has_recycler := TRUE
                "Ship Training Facility":
                  set has_ship_training := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 10
                "Fleet Training Facility":
                  set has_fleet_training := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 10
                "Subspace Transmitter":
                  set has_combat_mod := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 25
                "War Shrine":
                  set has_combat_mod := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 25
                "Events Predictor":
                  set has_combat_mod := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 25
                "Death Shrine":
                  set has_damage_mod := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 25
                "Dimensional Rift Projector":
                  set has_dimensional_rift := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 50
                "Psychic Ship Training Facility":
                  set has_ship_training := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 15
                "Psychic Fleet Training Facility":
                  set has_fleet_training := TRUE
                  set sobj_strategic_value := sobj_strategic_value + 15
              endcase
            endfor
          endif
        endif
      endif

      // Identify if we own the object or another player does
      if (sobj_owner > 0) then
        // Note space objects we own
        if (sobj_owner = sys_long_Player_ID) then
          // Determine the strength of the system
          set sys_strength := sys_strength + sobj_strength

          // Specific object lists
          case (sobj_type)
            SPACE_OBJECT_TYPE_PLANET:
              call lst_AI_Our_Colonies.add(sobj_id)
              call lst_AI_Our_Colony_Systems.set(sys_index, 1)
              call lst_AI_Our_System_Strengths.set(sys_index, sys_strength)
              set num_population := Sys_Get_Planet_Population(sobj_id)
              set sobj_strategic_value := sobj_strategic_value + Sys_Divide_Long(num_population, 100)
              set num_facilities := Get_Total_Number_Of_Facilities_On_Planet(sobj_id)
              set max_facilities := Sys_Divide_Long(Sys_Get_Space_Object_Facility_Total_Space(sobj_id), 1000)
              set this_strength := sobj_strength
              // Calculate pollution
              set pollution_pts := Calculate_Planet_Pollution(sobj_id)
              call lst_AI_Colony_Pollution_Pts.add(pollution_pts)
              // Empire level counts
              set lng_AI_Empire_Num_Colonies := lng_AI_Empire_Num_Colonies + 1
              set lng_AI_Empire_Num_Population := lng_AI_Empire_Num_Population + num_population
              set lng_AI_Empire_Num_Facilities := lng_AI_Empire_Num_Facilities + num_facilities
              set lng_AI_Empire_Num_Minerals_Facilities := lng_AI_Empire_Num_Minerals_Facilities + num_minerals
              set lng_AI_Empire_Num_Organics_Facilities := lng_AI_Empire_Num_Organics_Facilities + num_organics
              set lng_AI_Empire_Num_Radioactives_Facilities := lng_AI_Empire_Num_Radioactives_Facilities + num_radioactives
              set lng_AI_Empire_Num_Monolith_Facilities := lng_AI_Empire_Num_Monolith_Facilities + num_monoliths
              set lng_AI_Empire_Num_Research_Facilities := lng_AI_Empire_Num_Research_Facilities + num_research
              set lng_AI_Empire_Num_Intel_Facilities := lng_AI_Empire_Num_Intel_Facilities + num_intel
              // System level counts
              call lst_AI_Our_System_Colony_Count.set(sys_index, lst_AI_Our_System_Colony_Count.get(sys_index) + 1)
              call lst_AI_Our_System_Population.set(sys_index, lst_AI_Our_System_Population.get(sys_index) + num_population)
              call lst_AI_Our_System_Facility_Count.set(sys_index, lst_AI_Our_System_Facility_Count.get(sys_index) + num_facilities)
              call lst_AI_Our_System_Resource_Facility_Count.set(sys_index, lst_AI_Our_System_Resource_Facility_Count.get(sys_index) + num_minerals + num_organics + num_radioactives + num_monoliths)

              // Debug output
              call Sys_Debug_Print("System", "        - Colony Type = " + Sys_Get_Planet_Colony_Type(sobj_id))
              call Sys_Debug_Print("System", "        - Sobj ID = " + Sys_Convert_Long_To_String(sobj_id))
              call Sys_Debug_Print("System", "        - Location = " + Convert_Sector_To_String(sobj_sect))
              call Sys_Debug_Print("System", "          - Sys ID = " + Sys_Convert_Long_To_String(sys_index))
              call Sys_Debug_Print("System", "          - Sector ID = " + Sys_Convert_Long_To_String(sobj_sect))
              call Sys_Debug_Print("System", "        - # Population = " + Sys_Convert_Long_To_String(num_population) + "M")
              call Sys_Debug_Print("System", "        - # Facilities/Max Facilities = " + Sys_Convert_Long_To_String(num_facilities) + "/" + Sys_Convert_Long_To_String(max_facilities))
              call Sys_Debug_Print("System", "        - Pollution Points = " + Sys_Convert_Long_To_String(pollution_pts))

              // Note our core colonies
              if (sobj_strategic_value > 100) then
                call lst_AI_Our_Core_Colonies.add(sobj_id)
                // Debug output
                call Sys_Debug_Print("System", "        - Core colony")
              endif

              // Note our rioting colonies
              if Sys_Is_Planet_Rioting(sobj_id) then
                call lst_AI_Our_Rioting_Colonies.add(sobj_id)
                // Debug output
                call Sys_Debug_Print("System", "        - Rioting!")
              endif

              // Note our plagued colonies
              if (Sys_Get_Planet_Plague_Level(sobj_id) > 0) then
                call lst_AI_Our_Plagued_Colonies.add(sobj_id)
                // Debug output
                call Sys_Debug_Print("System", "        - Plagued!")
                if (not Is_Tech_Area_Available("Medical Treatment", Sys_Get_Planet_Plague_Level(sobj_id))) then
                  set bool_Race_Cannot_Cure_plague := TRUE
                  // Debug output
                  call Sys_Debug_Print("System", "        - No cure for plague!")
                endif
              endif

              // Add population to zero population colonies
              if (num_population = 0) then
                call lst_AI_Our_Zero_Population_Colonies.add(sobj_id)
                call Sys_Debug_Print("System", "        - No population!")
              endif

              // List our facilities
              // Debug output
              call Sys_Debug_Print("System", "        - Facility List:")
              call List_Planet_Facilities_For_Debug(sobj_id)

              // Note our Spaceport systems
              if (has_spaceport) then
                call lst_AI_Our_Spaceport_Systems.set(sys_index, 1)
              endif
              // Note our colonies with Space Yards
              if (has_space_yard) then
                call lst_AI_Our_Space_Yards.add(sobj_id)
                call lst_AI_Our_Space_Yard_Systems.set(sys_index, 1)
              endif
              // Note our colonies with Resupply Depots
              if (has_resupply_depot) then
                call lst_AI_Our_Resupply_Depots.add(sobj_id)
                call lst_AI_Our_Resupply_Systems.set(sys_index, 1)
              endif
              // Note our colonies with fleet training facilities
              if (has_fleet_training) then
                call lst_AI_Fleet_Training_System.add(sys_index)
                call lst_AI_Fleet_Training_Sector.add(sobj_sect)
              endif
              // Note our Ultra-Recycler systems
              if (has_recycler) then
                call lst_AI_Our_Recycler_Systems.set(sys_index, 1)
              endif

              // Add our colonies strategic value to the system combined strategic value
              call lst_AI_Our_System_Strategic_Values.set(sys_index, sobj_strategic_value + lst_AI_Our_System_Strategic_Values.get(sys_index))
              
              // Debug output
              call Sys_Debug_Print("System", "        - Strength = " + Sys_convert_Long_To_String(this_strength))
              call Sys_Debug_Print("System", "        - Strategic value = " + Sys_convert_Long_To_String(sobj_strategic_value))

              // Identify our most important colony in each of our systems
              if (sobj_strategic_value > max_strategic_value) then
                set max_strategic_value := sobj_strategic_value
                // Set this colony to be our system capital
                call lst_AI_Our_System_Capital_Colonies.set(sys_index, sobj_id)
              endif

              // Make our most strategically valued planets fleet hubs
              if (has_space_yard) and (has_resupply_depot) and (sobj_strategic_value > 50) then
                call lst_AI_Our_Fleet_Hubs.add(sobj_id)
                call lst_AI_Fleet_Hub_System.add(sys_index)
                call lst_AI_Fleet_Hub_Sector.add(sobj_sect)
              endif

            SPACE_OBJECT_TYPE_SHIP:
              // Get our ship's details
              set ai_design_type := Sys_Get_Space_Object_Ai_Design_Type(sobj_id)
              set sobj_tonnage := Sys_Get_Space_Object_Total_Tonnage_Structure(sobj_id)
              set sobj_dmg_tonnage := Sys_Get_Space_Object_Total_Tonnage_Damage(sobj_id)
              set sobj_dmg_pct := Sys_Trunc(sobj_dmg_tonnage / sobj_tonnage * 100)

              // Add to our list of ships
              set lng_AI_Empire_Num_Ships := lng_AI_Empire_Num_Ships + 1
              call lst_AI_Our_Ships.add(sobj_id)
              call lst_AI_Our_Ships_Status.add(0)

              // Note our system total ship strength
              set sys_strength := lst_AI_Our_System_Ship_Strengths.get(sys_index)
              call lst_AI_Our_System_Ship_Strengths.set(sys_index, sys_strength + sobj_strength)

              // Note our base locations
              if Sys_Is_Space_Object_Base(sobj_id) then
                // Note our Defense Base strength
                if (ai_design_type = "Base (Defense S)" or ai_design_type = "Base (Defense L)") then
                  call lst_AI_Our_System_Defense_Base_Strengths.set(sys_index, lst_AI_Our_System_Defense_Base_Strengths.get(sys_index) + sobj_strength)
                endif
                // Note our colonies with Space Yard Bases
                if (ai_design_type = "Base (Space Yard)") then
                  set planet_id := Get_Colony_In_Location(sys_index, sobj_sect)
                  // Make a list of our Space Yard Hubs
                  if (planet_id > 0) then
                    if (lst_AI_Our_Space_Yard_Hubs.indexof(planet_id) <= 0) then
                      call lst_AI_Our_Space_Yard_Hubs.add(planet_id)
                    endif
                  endif
                endif
              endif

              // Note our repairing or disabled ships
              if (sobj_dmg_pct > 5) then
                if Sys_Is_Space_Yard_At_Location(sys_long_Player_ID, sys_index, sobj_sect) then
                  call lst_AI_Our_Repairing_Ships.add(sobj_id)
                  // Track locations where damage ships are accumulated
                  call add_location(LOCATION_TYPE_REPAIR, sys_index, sobj_sect, sobj_dmg_tonnage, 0, 0, 0, 0, 0)
                else
                  if (sobj_movement = 0) then
                    call lst_AI_Our_Disabled_Ships.add(sobj_id)
                  endif
                endif
              endif

              // Note our maximum ship speed
              if (sobj_movement > lng_Max_Ship_Movement) then
                set lng_Max_Ship_Movement := sobj_movement
              endif

              // Convert our design's AI design type to a general AI design type
              set ai_design_type := Get_General_AI_Design_Type(ai_design_type)

              // Determine the number of combat ships we have
              if (ai_design_type = "Ship (Attack)") or (ai_design_type = "Ship (Defense)") or (ai_design_type = "Ship (Seeker)") then
                set lng_AI_Num_Combat_Ships := lng_AI_Num_Combat_Ships + 1
              endif

              // Determine the number of ships we have for our fleets
              if (ai_design_type = "Ship (Attack)") or (ai_design_type = "Ship (Defense)") or (ai_design_type = "Ship (Seeker)") or (ai_design_type = "Ship (Attack Base)") or (ai_design_type = "Ship (Point-Defense)") or (ai_design_type = "Ship (Bombardment)") or (ai_design_type = "Ship (Boarding)") or (ai_design_type = "Ship (Kamikaze)") or (ai_design_type = "Carrier (Fighter)") or (ai_design_type = "Carrier (Drone)") or (ai_design_type = "Transport (Troop)") or (ai_design_type = "Transport (Supply)") or (ai_design_type = "Ship (Mine Sweeper)") or (ai_design_type = "Ship (Repair)") then
                if ((Sys_Get_Space_Object_Number_Of_Damaged_Components(sobj_id) / Sys_Get_Space_Object_Comp_Count(sobj_id)) < 0.25) then
                  set lng_AI_Available_Fleet_Ships_Count := lng_AI_Available_Fleet_Ships_Count + 1
                endif
              endif

              // Determine the number of Mine Sweepers we have
              if (ai_design_type = "Ship (Mine Sweeper)") then
                set lng_Num_Minesweepers := lng_Num_Minesweepers + 1
                // Designate some Mine Sweepers for solitary use
                if (Sys_Get_Space_Object_Fleet(sobj_id) = 0) and (Sys_Get_Space_Object_Order_Type(sobj_id, 1) <> ORDER_JOIN_FLEET) then
                  if (lst_AI_Solitary_Minesweepers.count() < lng_Num_Minesweepers * 0.1) or (lst_AI_Solitary_Minesweepers.count() < 1) then
                    call lst_AI_Solitary_Minesweepers.add(sobj_id)
                  endif
                endif
              endif

              // Compile a list of our ships to escort/protect
              if (is_enemy_present) then
                if (ai_design_type = "Ship (Warp Point Opener)") or (ai_design_type = "Ship (Warp Point Closer)") or (ai_design_type = "Ship (Planet Creator)") or (ai_design_type = "Ship (Planet Destroyer)")  or (ai_design_type = "Colonizer (Rock)") or (ai_design_type = "Colonizer (Ice)") or (ai_design_type = "Colonizer (Gas Giant)") then
                  call lst_AI_Our_Ships_To_Escort.add(sobj_id)
                endif
              endif
              if (ai_design_type = "Ship (Star Destroyer)") or (ai_design_type = "Ship (Black Hole Creator)") or (ai_design_type = "Ship (Nebulae Creator)") then
                call lst_AI_Our_Ships_To_Escort.add(sobj_id)
              endif

            SPACE_OBJECT_TYPE_UNITGROUP:
              // Add to our list of vehicles
              call lst_AI_Our_Ships.add(sobj_id)
              call lst_AI_Our_Ships_Status.add(0)
              // Add to our list of system strengths
              set sys_strength := lst_AI_Our_System_Unit_Strengths.get(sys_index)
              call lst_AI_Our_System_Unit_Strengths.set(sys_index, sys_strength + sobj_strength)
          endcase
        else
          // Mark locations for anger
          if (plr_has_colony) then
            set sys_has_visible_other := TRUE
            // Colonies
            if (sobj_type = SPACE_OBJECT_TYPE_PLANET) then
              call lst_AI_Anger_Location_Colony_Count.set(sobj_owner, lst_AI_Anger_Location_Colony_Count.get(sobj_owner) + 1)
            endif
            // Ships
            if (sobj_type = SPACE_OBJECT_TYPE_SHIP) then 
              call lst_AI_Anger_Location_Ship_Count.set(sobj_owner, lst_AI_Anger_Location_Ship_Count.get(sobj_owner) + 1)
              // Mark strategic value with extra value for certain types
              if (Sys_Pos_String("Colonizer", ai_design_type) > 0) or (Sys_Pos_String("Remote", ai_design_type) > 0) or (Sys_Pos_String("Transport", ai_design_type) > 0) then
                set sobj_strategic_value := 5
              else
                set sobj_strategic_value := 2
              endif
            endif
            // Unit groups
            if (sobj_type = SPACE_OBJECT_TYPE_UNITGROUP) then
              call lst_AI_Anger_Location_Unit_Count.set(sobj_owner, lst_AI_Anger_Location_Unit_Count.get(sobj_owner) + 1)
              set sobj_strategic_value := 5
            endif
          endif
          // Mark locations and strengths for enemies
          if Sys_Empire_Politics_Is_Player_Enemy(sys_long_Player_ID, sobj_owner) or (lst_Politics_Non_Aggression_Level.get(sobj_owner) < NON_AGGRESSION_ALL_SYSTEMS) then
            set sys_has_visible_enemy := TRUE
            // Add object strength to enemies' system strength
            call lst_AI_Enemy_System_Strengths.set(sys_index, lst_AI_Enemy_System_Strengths.get(sys_index) + sobj_strength)
            // Mark defense locations where we have colonies and there are enemy objects about
            if (plr_has_colony) then
              call Add_Location(LOCATION_TYPE_DEFENSE, sys_index, sobj_sect, sobj_strength, sobj_owner, sys_dist, sobj_id, sobj_movement, sobj_strategic_value)
            endif
            // Enemy colonies
            if (sobj_type = SPACE_OBJECT_TYPE_PLANET) then
              // Debug output
              call Sys_Debug_Print("System", "        - Enemy Colony! (" + Sys_Empire_Get_Empire_Name(sobj_owner) + ")")
              // Add to list of enemy colony systems
              call lst_AI_Enemy_Colony_Systems.set(sys_index, 1)
              call lst_AI_Enemy_System_Colony_Count.set(sys_index, lst_AI_Enemy_System_Colony_Count.get(sys_index) + 1)
              // Add to enemy System facility counts
              call lst_AI_Enemy_System_Facility_Count.set(sys_index, lst_AI_Enemy_System_Facility_Count.get(sys_index) + num_facilities)
              call lst_AI_Enemy_System_Facility_Count.set(sys_index, lst_AI_Enemy_System_Facility_Count.get(sys_index) + num_facilities)
              call lst_AI_Enemy_System_Population.set(sys_index, lst_AI_Enemy_System_Population.get(sys_index) + num_population)
              // Note the colony's strategic value
              call lst_AI_Enemy_System_Strategic_Values.set(sys_index, sobj_strategic_value + lst_AI_Enemy_System_Strategic_Values.get(sys_index))
              // Reduce apparent strength of valuable enemy colonies to improve targeting
              if Is_Facility_Present_On_Planet(sobj_id, lng_facility_id_spaceport) then
                set sobj_strength := Sys_Divide_Long(sobj_strength, 2)
              endif
              // Mark our enemy colonies as attack locations in our non-colony systems
              if (not plr_has_colony) and (lst_Politics_Non_Aggression_Level.get(sobj_owner) = NON_AGGRESSION_NONE) then
                call Add_Location(LOCATION_TYPE_ATTACK, sys_index, sobj_sect, sobj_strength, sobj_owner, sys_dist, sobj_id, sobj_movement, sobj_strategic_value)
              endif
              // Mark (vulnerable) enemy breathable planets as targets for planet destruction
              if (sobj_strength < 3000) then
                // Only add planets that we have the technology to destroy
                if (Get_Empire_Tech_Level_By_Name("Stellar Manipulation") - 5 >= Get_Planet_Size(sobj_id)) then
                  call lst_AI_Planets_To_Destroy.add(sobj_id)
                  call lst_AI_Planets_To_Destroy_Distance.add(sys_dist)
                endif
              endif
            else
              // Mark our enemy ships/unit groups as nearby enemies in non-colony systems
              if (not plr_has_colony) and (lst_Politics_Non_Aggression_Level.get(sobj_owner) = NON_AGGRESSION_NONE) then
                call Add_Location(LOCATION_TYPE_NEARBY_ENEMY, sys_index, sobj_sect, sobj_strength, sobj_owner, sys_dist, sobj_id, sobj_movement, sobj_strategic_value)
              endif
              // Enemy ships
              if (sobj_type = SPACE_OBJECT_TYPE_SHIP) then
                // Do we suspect the ship is planning to attack an object that belongs to us?
                if (Sys_Get_Space_Object_Order_Count(sobj_id) > 0) then
                  if (Sys_Get_Space_Object_Order_Type(sobj_id, 1) = ORDER_ATTACK_MOVE) then
                    set target_sobj_id := Sys_Get_Space_Object_Order_Target_Space_Object_ID(sobj_id, 1)
                    set target_sys_loc := Sys_Get_Space_Object_Order_System_Location(sobj_id, 1)
                    set target_sect_loc := Sys_Get_Space_Object_Order_Sector_Location(sobj_id, 1)
                    set target_sobj_dist := Sys_Get_Hex_Distance_Between_Sectors(sobj_owner, sys_index, sobj_sect, target_sys_loc, target_sect_loc)
                    // If no target object id, then check for a colony in location
                    if (target_sobj_id = 0) then
                      set target_sobj_id := Get_Colony_In_Location(target_sys_loc, target_sect_loc)
                    endif
                    // Is it attacking an object that belongs to us?
                    if (Sys_Get_Space_Object_Owner(target_sobj_id) = sys_long_Player_ID) then
                      // Add it to a list of targets to actively defend based on our difficulty level
                      if (Sys_Get_Random_Long(1, 3) < lng_AI_Difficulty) then
                        // Is the target location unique?
                        set target_index := lst_AI_Target_To_Defend.indexof(target_sobj_id)
                        if (target_index > 0) then
                          // Add to the target count if object has been targeted previously
                          set target_count := lst_AI_Target_To_Defend_Count.get(target_index) + 1
                          call lst_AI_Target_To_Defend_Count.set(target_index, target_count)
                        else
                          // Add it to our list of suspected targets
                          call lst_AI_Target_To_Defend.add(target_sobj_id)
                          call lst_AI_Target_To_Defend_Dist.add(target_sobj_dist)
                          call lst_AI_Target_To_Defend_Count.add(1)
                        endif
                      endif
                    endif
                  endif
                endif
                // Note our enemies' maximum ship speed
                if (sobj_movement > lng_Max_Enemy_Ship_Movement) then
                  set lng_Max_Enemy_Ship_Movement := sobj_movement
                endif
                // Note our enemy's armor/shield ratio
                if (Sys_Get_Space_Object_Total_Weapon_Damage(sobj_id) > 0) then
                  set total_enemy_armor := total_enemy_armor + Sys_Get_Space_Object_Current_Armor_Structure(sobj_id)
                  set total_enemy_shields := total_enemy_shields + Sys_Get_Space_Object_Current_Shields(sobj_id)
                  // Note the overall ratio
                  if (total_enemy_armor > (total_enemy_shields * 1.1)) then
                    set bool_Enemy_Uses_Mostly_Armor := TRUE
                  else
                    set bool_Enemy_Uses_Mostly_Armor := FALSE
                    set bool_Enemy_Uses_Mostly_Shields := TRUE
                  endif
                endif
              endif
            endif
          else
            // Note our friends armor/shield ratio
            if (Sys_Get_Space_Object_Total_Weapon_Damage(sobj_id) > 0) then
              set total_friend_armor := total_friend_armor + Sys_Get_Space_Object_Current_Armor_Structure(sobj_id)
              set total_friend_shields := total_friend_shields + Sys_Get_Space_Object_Current_Shields(sobj_id)
              // Note the overall ratio
              if (total_friend_armor > (total_friend_shields * 1.1)) then
                set bool_Friend_Uses_Mostly_Armor := TRUE
              else
                set bool_Friend_Uses_Mostly_Armor := FALSE
                set bool_Friend_Uses_Mostly_Shields := TRUE
              endif
            endif
          endif
          // Note our allied strengths
          if Sys_Empire_Politics_Is_Player_Ally(sys_long_Player_ID, sobj_owner) then
            call lst_AI_Ally_System_Strengths.set(sys_index, sobj_strength)
             // Note our mutual colony locations
            if (sobj_type = SPACE_OBJECT_TYPE_PLANET) then
              // Debug output
              call Sys_Debug_Print("System", "        - Ally Colony! (" + Sys_Empire_Get_Empire_Name(sobj_owner) + ")")
              if (plr_has_colony) and (lst_AI_Mutual_Colony_Systems.indexof(sys_index) <= 0) then
                call lst_AI_Mutual_Colony_Systems.add(sys_index)
              endif
            endif
          endif
        endif
      else
        // Debug output for all other objects
        if (sobj_type <> SPACE_OBJECT_TYPE_PLANET) then
          call Sys_Debug_Print("System", "      - " + Sys_Get_Space_Object_Name(sobj_id) + " " + Convert_Sector_To_String(sobj_sect))
          call Sys_Debug_Print("System", "        - Sobj ID = " + Sys_Convert_Long_To_String(sobj_id))
        endif
      endif
    endfor
  endif

  // Mark systems to destroy
  if (sobj_type = SPACE_OBJECT_TYPE_STAR) then
    if (lst_AI_Our_System_Strengths.get(sys_index) <= 0) and (lst_AI_Ally_System_Strengths.get(sys_index) <= 0) then
      if Does_Enemy_Have_Colony_In_System(sys_index) then
        call lst_AI_Stars_To_Destroy.add(sobj_id)
        call lst_AI_Stars_To_Destroy_Distance.add(sys_dist)
      endif
    endif
  endif

  // Is there no star in this system?
  if (not is_star_present) then
    // Debug output
    call Sys_Debug_Print("Lists", "    - No Star")
  endif

  // Is this system a Nebula?
  if Is_System_Nebula(sys_index) then
    // Add to our list of nebulae
    call lst_AI_Nebula_Sys.add(sys_index)
    // Debug output
    call Sys_Debug_Print("Lists", "    - Nebula system")
  endif

  // Is this system a Blackhole?
  if Is_System_Blackhole(sys_index) then
    // Add to our list of blackholes
    call lst_AI_Blackhole_Sys.add(sys_index)
    // Debug output
    call Sys_Debug_Print("Lists", "    - Black hole system")
  endif

  // Note the number of planets
  call Sys_Debug_Print("Lists", "  - System # Planets = " + Sys_Convert_Long_To_String(sys_planet_count))
  // Note systems with fewer than 3 planets
  if (sys_planet_count < 3) then
    call lst_AI_Systems_With_Limited_Planets.add(sys_index)
    // Debug output
    call Sys_Debug_Print("Lists", "    - Limited planet system")
  endif

  // Check for unseen sectors in the solar system
  if Sys_Does_System_Have_Unexplored_Sectors(sys_long_Player_ID, sys_index) then
    set ships_in_transit := Sys_Get_Number_Of_Ships_With_Order(sys_long_Player_ID, ORDER_SURVEY_SYSTEM, sys_index, 0, 0)
    // Add to our list of systems to survey if no other ships are in transit and our enemies do not have colonies in the system
    if (ships_in_transit = 0) and (sys_dist < 999) and (not Does_Enemy_Have_Colony_In_System(sys_index)) then
      if (sys_dist_to_colony < 100) then
        set sys_dist := Sys_Get_System_Distance_Between_Solar_Systems(sys_index, home_sys_loc)
        call lst_AI_Survey_System.add(sys_index)
        call lst_AI_Survey_Distance_To_Home.add(sys_dist)
      endif
    endif
  endif

  // Were there hidden enemies in our colony system?
  if (is_enemy_present) and (plr_has_colony) then
    if (not sys_has_visible_enemy) then
      call lst_AI_Hidden_Enemy_System.add(sys_index)
      // Debug output
      call Sys_Debug_Print("Lists", "    - Hidden Enemy!")
    endif
  endif
  // Were there other hidden players in our colony system?
  if (are_others_present) and (plr_has_colony) then
    if (not sys_has_visible_other) then
      call lst_AI_Hidden_Other_System.add(sys_index)
      // Debug output
      call Sys_Debug_Print("Lists", "    - Hidden Other!")
    endif
  endif

  // Add additional strategic value for warp point connectivity
  set sys_strategic_value := (lst_AI_System_Chokepoint_Rating.get(sys_index) * 1) + (lst_AI_System_WP_Connections.get(sys_index) * 10)
  // Update the system's strategic value
  call lst_AI_Our_System_Strategic_Values.set(sys_index, lst_AI_Our_System_Strategic_Values.get(sys_index) + sys_strategic_value)
  call lst_AI_Enemy_System_Strategic_Values.set(sys_index, lst_AI_Enemy_System_Strategic_Values.get(sys_index) + sys_strategic_value)

  // Check warp point connections
  set wp_count := Sys_Get_Solar_System_Warp_Point_Count(sys_long_Player_ID, sys_index)

  // Classify systems based on the number of warp points
  if (wp_count <= 0) then
    call lst_AI_Our_Not_Connected_Systems.add(sys_index)
  endif
  if (wp_count = 1) then
    call lst_AI_System_WP_Deadends.set(sys_index, 1)
  endif
  if (wp_count = 2) then
    call lst_AI_System_Two_WPs.set(sys_index, 1)
  endif
  if (wp_count >= 3) then
    call lst_AI_System_WP_Hubs.set(sys_index, 1)
  endif

  // Base system priority scoring
  set def_sys_priority_score := def_sys_priority_score + lst_AI_Our_System_Colony_Count * 1000
  set def_sys_priority_score := def_sys_priority_score + lst_AI_Our_System_Population.get(sys_index)
  set def_sys_priority_score := def_sys_priority_score + lst_AI_Our_System_Facility_Count.get(sys_index) * 50
  set def_sys_priority_score := def_sys_priority_score + lst_AI_Our_System_Resource_Facility_Count.get(sys_index) * 100
  set def_sys_priority_score := def_sys_priority_score + lst_AI_Our_System_Strategic_Values.get(sys_index) * 100
  call lst_AI_Defense_System_Priority_Rating.set(sys_index, def_sys_priority_score)

  set atk_sys_priority_score := atk_sys_priority_score + lst_AI_Enemy_System_Colony_Count.get(sys_index) * 1000
  set atk_sys_priority_score := atk_sys_priority_score + lst_AI_Enemy_System_Population.get(sys_index)
  set atk_sys_priority_score := atk_sys_priority_score + lst_AI_Enemy_System_Facility_Count.get(sys_index) * 50
  set atk_sys_priority_score := atk_sys_priority_score + lst_AI_Enemy_System_Resource_Facility_Count.get(sys_index) * 100
  set atk_sys_priority_score := atk_sys_priority_score + lst_AI_Enemy_System_Strategic_Values.get(sys_index) * 100
  call lst_AI_Attack_System_Priority_Rating.set(sys_index, atk_sys_priority_score)

  // Is the system generally valuable?
  if (sys_strategic_value > 200) then
    set loc_priority := loc_priority + 1
  endif

  // Is this system an important transit point?
  if (lst_AI_System_Chokepoint_Rating.get(sys_index) > 50) or (wp_count > 2) then
    set loc_priority := loc_priority + 1
    // Add extra priority if the system is a more significant chokepoint
    if (lst_AI_System_Chokepoint_Rating.get(sys_index) > 70) then
      set loc_priority := loc_priority + 1
    endif
  endif

  // Check to see if this system leads to other systems that don't have our colonies
  if (not Do_We_Have_Colonies_In_All_Connected_Systems(sys_index)) then
    set is_border_system := TRUE
  endif

  // Add to list of unexplored systems if...
  if (wp_count > 0) then
    for wp_index := 1 to wp_count do
      set dest_sys := Sys_Get_Solar_System_Warp_Point_Dest_System(sys_long_Player_ID, sys_index, wp_index)
      set wp_sect := Sys_Get_Solar_System_Warp_Point_Sector(sys_long_Player_ID, sys_index, wp_index)
      set wp_id := Sys_Get_Solar_System_Warp_Point_ID(sys_long_Player_ID, sys_index, wp_index)
      set sys_dist := Sys_Get_System_Distance_Between_Solar_Systems(sys_index, home_sys_loc)

      // Add this system to our list of systems to explore if we have not seen it and no enemies are on the other side of the warp point
      if (not Sys_Have_Seen_System(sys_long_Player_ID, dest_sys)) and (not Is_Enemy_Present_At_Destination_WP(sys_index, dest_sys)) then
        set ships_in_transit := Sys_Get_Number_Of_Ships_With_Order(sys_long_Player_ID, ORDER_MOVETO, sys_index, wp_sect, 0)

        if (ships_in_transit = 0) and (sys_dist < 999) then
          if (sys_dist < 999) and (sys_dist_to_colony < 100) then
            call lst_AI_Explore_System.add(sys_index)
            call lst_AI_Explore_Sector.add(wp_sect)
            call lst_AI_Explore_WP_Index.add(wp_index)
            call lst_AI_Explore_WP_ID.add(wp_id)
            call lst_AI_Explore_Distance_To_Home.add(sys_dist)
          endif
        endif
      endif

      // If one of the connecting systems is a blackhole or nebula, check for colony systems connected to them
      if Is_System_Nebula(dest_sys) or Is_System_Blackhole(dest_sys) then
        if Do_We_Have_Colonies_In_All_Connected_Systems(dest_sys) then
          set is_border_system := FALSE
        endif
      endif

      // Warp Points to unseen systems or systems with enemies
      if (plr_has_colony) then
        set dest_sys_enemy := Does_Enemy_Have_Colony_In_System(dest_sys)
        set dest_sys_wp_enemy := Is_Enemy_Present_At_Destination_WP(sys_index, dest_sys)
        set dest_sys_no_colonies := (not Sys_Player_Has_Colony_In_System(sys_long_Player_ID, dest_sys))
        set dest_sys_empty := Is_System_Blackhole(dest_sys) or Does_System_Have_No_Planets(dest_sys) or Does_System_Have_No_Planets(dest_sys)

        if (dest_sys_enemy) or (dest_sys_wp_enemy) or (dest_sys_no_colonies) or (dest_sys_empty) then
          if (is_border_system) then
            if (dest_sys_no_colonies) then
              // Add location for dropping units
              call lst_AI_Unit_Drop_Location_System.add(sys_index)
              call lst_AI_Unit_Drop_Location_Sector.add(wp_sect)
              call lst_AI_Unit_Drop_Location_Dest_Sys.add(dest_sys)
              // Add location for remote bases
              call lst_AI_Remote_Base_Location_System.add(sys_index)
              call lst_AI_Remote_Base_Location_Sector.add(wp_sect)
              // Set location priority
              if (dest_sys_enemy) and (lst_AI_Enemy_System_Strengths.get(dest_sys) > 10000) then
                call lst_AI_Unit_Drop_Location_Priority.add(LOCATION_PRIORITY_HIGH)
                call lst_AI_Remote_Base_Location_Priority.add(LOCATION_PRIORITY_HIGH)
              else
                if (dest_sys_empty) then
                  call lst_AI_Unit_Drop_Location_Priority.add(LOCATION_PRIORITY_LOW)
                  call lst_AI_Remote_Base_Location_Priority.add(LOCATION_PRIORITY_LOW)
                else
                  call lst_AI_Unit_Drop_Location_Priority.add(LOCATION_PRIORITY_MEDIUM)
                  call lst_AI_Remote_Base_Location_Priority.add(LOCATION_PRIORITY_MEDIUM)
                endif
              endif
              // Add location for primary patrols and the corresponding destination location
              call lst_AI_Patrol_System.add(sys_index)
              call lst_AI_Patrol_Sector.add(wp_sect)
              call lst_AI_Patrol_WP_ID.add(wp_id)
              call lst_AI_Patrol_Dest_System.add(dest_sys)
              call lst_AI_Patrol_Dest_Sector.add(Get_Destination_WP_Sector(sys_index, dest_sys))
            endif
          endif
          // Is the enemy present in the destination system?
          if (dest_sys_enemy) or (dest_sys_wp_enemy) then
            // Add location priority for the connection to an enemy system
            set loc_priority := loc_priority + 1
            // Add another location priority point if the destination system is a transit hub/strategic chokepoint
            if (lst_AI_System_Chokepoint_Rating.get(dest_sys) > 50) or (Sys_Get_Solar_System_Warp_Point_Count(sys_long_Player_ID, dest_sys) > 2) then
              set loc_priority := loc_priority + 1
            endif
            // Add extra priority if this is a hard chokepoint
            if (wp_count = 2) then
              set loc_priority := loc_priority + 2
            endif
            // Set final location priority level
            if (loc_priority = 1) then
              set loc_priority := LOCATION_PRIORITY_LOW
            else
              if (loc_priority <= 4) then
                set loc_priority := LOCATION_PRIORITY_MEDIUM
              else
                set loc_priority := LOCATION_PRIORITY_HIGH
              endif
            endif
            // Add location for fleets to hold at
            call lst_AI_Fleet_Holding_System.add(sys_index)
            call lst_AI_Fleet_Holding_Sector.add(wp_sect)
            call lst_AI_Fleet_Holding_WP_ID.add(wp_id)
            call lst_AI_Fleet_Holding_Dest_System.add(dest_sys)
            call lst_AI_Fleet_Holding_Location_Priority.add(loc_priority)
            // Add destination system as a system to avoid for non-combat ships
            if (lst_AI_System_To_Avoid.indexof(dest_sys) <= 0) then
              call lst_AI_System_To_Avoid.add(dest_sys)
            endif
          endif
          // Add enemy systems to our list of warp points to close
          If (lst_AI_Enemy_Colony_Systems.indexof(dest_sys) > 0) then
            call lst_AI_Warp_Points_To_Close_ID.add(wp_id)
            call lst_AI_Warp_Points_To_Close_Dest_Sys.add(dest_sys)
          endif
        else
          // Add location for secondary patrols
          if (not Sys_Player_Has_Colony_In_System(sys_long_Player_ID, dest_sys)) then
            call lst_AI_Patrol_Secondary_System.add(sys_index)
            call lst_AI_Patrol_Secondary_Sector.add(wp_sect)
          endif
        endif
      else
        // Mark all other systems with enemies as systems to avoid for non-combat ships
        if Does_Enemy_Have_Colony_In_System(sys_index) then
          if (lst_AI_System_To_Avoid.indexof(sys_index) <= 0) then
            call lst_AI_System_To_Avoid.add(sys_index)
          endif
        endif
      endif
    endfor
    // Mark our border systems
    if (is_border_system) and (plr_has_colony) then
      call lst_AI_Border_System.add(sys_index)
    endif
  endif

  // Is the system protected?
  set our_strength := lst_AI_Our_System_Ship_Strengths.get(sys_index) + lst_AI_Our_System_Unit_Strengths.get(sys_index) + lst_AI_Our_System_Defense_Base_Strengths.get(sys_index)
  set enemy_strength := Sys_Multiply_Long(lst_AI_Enemy_System_Strengths.get(sys_index), 2)

  if (our_strength > enemy_strength) then
    call lst_AI_Our_Protected_Systems.set(sys_index, 1)
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Add_Location
//------------------------------------------------------------------------
function Add_Location returns boolean
params
  location_type:             long
  sobj_sys:                  long
  sobj_sect:                 long
  sobj_strength:             long
  sobj_owner:                long
  sys_dist:                  long
  sobj_id:                   long
  sobj_movement:             long
  sobj_strategic_value:      long
vars
  found_pos:                 long
  list_count:                long
  index:                     long
  min_movement:              long := 0
  max_range_mod:             long := 0
  total_count:               long := 0
  skip_add_location:         boolean := FALSE
begin

  // Do we already have ships that are set to attack this sector?
  set skip_add_location := (Sys_Get_Number_Of_Ships_With_Order(sys_long_Player_ID, ORDER_ATTACK_MOVE, sobj_sys, sobj_sect, 0) > 0)

  // Is the location too far to worry about now?
  if (bool_AI_Team_Mode_On) then
    set max_range_mod := max_range_mod + 70
  endif

  if (not skip_add_location) then
    set skip_add_location := (lst_AI_System_Distance_To_Nearest_Colony.get(sobj_sys) > 100)
  endif

  if (sobj_movement > min_movement) then
    set min_movement := sobj_movement
  endif

  // Add a location
  if (not skip_add_location) then
    case (location_type)
      // Location - Defense
      // Enemy ships or colonies in systems where we have colonies
      LOCATION_TYPE_DEFENSE:
        set found_pos := 0
        set list_count := lst_AI_Defense_Location_System.count()

        if (list_count > 0) then
          for index := 1 to list_count do
            if (found_pos = 0) then
              if (lst_AI_Defense_Location_System.get(index) = sobj_sys) and (lst_AI_Defense_Location_Sector.get(index) = sobj_sect) then
                set found_pos := index
              endif
            endif
          endfor
        endif

        if (found_pos > 0) then
          call lst_AI_Defense_Location_Strength.set(found_pos, lst_AI_Defense_Location_Strength.get(found_pos) + sobj_strength)
          call lst_AI_Defense_Location_Ship_Count.set(found_pos, lst_AI_Defense_Location_Ship_Count.get(found_pos) + 1)
          call lst_AI_Defense_Location_Strategic_Value.set(found_pos, lst_AI_Defense_Location_Strategic_Value.get(found_pos) + sobj_strategic_value)
          // Determine the minimum movement amount of the targets
          set min_movement := lst_AI_Defense_Location_Ship_Min_Move.get(found_pos)
          // Update the minimum movement
          if (sobj_movement < min_movement) then
            call lst_AI_Defense_Location_Ship_Min_Move.set(found_pos, sobj_movement)
          else
            call lst_AI_Defense_Location_Ship_Min_Move.set(found_pos, min_movement)
          endif
        else
          call lst_AI_Defense_Location_System.add(sobj_sys)
          call lst_AI_Defense_Location_Sector.add(sobj_sect)
          call lst_AI_Defense_Location_Strength.add(sobj_strength)
          call lst_AI_Defense_Location_Assigned_Strength.add(0)
          call lst_AI_Defense_Location_Owner.add(sobj_owner)
          call lst_AI_Defense_Location_System_Distance.add(sys_dist)
          call lst_AI_Defense_Location_Ship_ID.add(sobj_id)
          call lst_AI_Defense_Location_Ship_Count.add(1)
          call lst_AI_Defense_Location_Ship_Min_Move.add(sobj_movement)
          call lst_AI_Defense_Location_Strategic_Value.add(sobj_strategic_value)
        endif

        // Add to our total count for the system
        set total_count := 1 + lst_AI_Defense_System_Total_Count.get(sobj_sys)
        call lst_AI_Defense_System_Total_Count.set(sobj_sys, total_count)

      // Location - Attack
      // Enemy colonies in systems where we don't have colonies
      LOCATION_TYPE_ATTACK:
        set found_pos := 0
        set list_count := lst_AI_Attack_Location_System.count()

        if (list_count > 0) then
          for index := 1 to list_count do
            if (found_pos = 0) then
              if (lst_AI_Attack_Location_System.get(index) = sobj_sys) and (lst_AI_Attack_Location_Sector.get(index) = sobj_sect) then
                set found_pos := index
              endif
            endif
          endfor
        endif

        if (found_pos > 0) then
          call lst_AI_Attack_Location_Strength.set(found_pos, lst_AI_Attack_Location_Strength.get(found_pos) + sobj_strength)
          call lst_AI_Attack_Location_Ship_Count.set(found_pos, lst_AI_Attack_Location_Ship_Count.get(found_pos) + 1)
          call lst_AI_Attack_Location_Strategic_Value.set(found_pos, lst_AI_Attack_Location_Strategic_Value.get(found_pos) + sobj_strategic_value)
        else
          call lst_AI_Attack_Location_System.add(sobj_sys)
          call lst_AI_Attack_Location_Sector.add(sobj_sect)
          call lst_AI_Attack_Location_Strength.add(sobj_strength)
          call lst_AI_Attack_Location_Assigned_Strength.add(0)
          call lst_AI_Attack_Location_Owner.add(sobj_owner)
          call lst_AI_Attack_Location_System_Distance.add(sys_dist)
          call lst_AI_Attack_Location_Ship_ID.add(sobj_id)
          call lst_AI_Attack_Location_Ship_Count.add(1)
          call lst_AI_Attack_Location_Strategic_Value.add(sobj_strategic_value)
        endif

        // Add to our total count for the system
        set total_count := 1 + lst_AI_Attack_System_Total_Count.get(sobj_sys)
        call lst_AI_Attack_System_Total_Count.set(sobj_sys, 1)

      // Location - Nearby Enemy
      // Enemy ships in systems where we don't have colonies
      LOCATION_TYPE_NEARBY_ENEMY:
        set found_pos := 0
        set list_count := lst_AI_Nearby_Enemy_Location_System.count()

        if (list_count > 0) then
          for index := 1 to list_count do
            if (found_pos = 0) then
              if (lst_AI_Nearby_Enemy_Location_System.Get(index) = sobj_sys) and (lst_AI_Nearby_Enemy_Location_Sector.get(index) = sobj_sect) then
                set found_pos := index
              endif
            endif
          endfor
        endif

        if (found_pos > 0) then
          call lst_AI_Nearby_Enemy_Location_Strength.set(found_pos, lst_AI_Nearby_Enemy_Location_Strength.get(found_pos) + sobj_strength)
          call lst_AI_Nearby_Enemy_Location_Ship_Count.set(found_pos, lst_AI_Nearby_Enemy_Location_Ship_Count.get(found_pos) + 1)
          call lst_AI_Nearby_Enemy_Location_Strategic_Value.set(found_pos, lst_AI_Nearby_Enemy_Location_Strategic_Value.get(found_pos) + sobj_strategic_value)
          // Determine the minimum movement amount of the targets
          set min_movement := lst_AI_Nearby_Enemy_Location_Ship_Min_Move.get(found_pos)
          // Update the minimum movement
          if (sobj_movement < min_movement) then
            call lst_AI_Nearby_Enemy_Location_Ship_Min_Move.set(found_pos, sobj_movement)
          else
            call lst_AI_Nearby_Enemy_Location_Ship_Min_Move.set(found_pos, min_movement)
          endif
        else
          call lst_AI_Nearby_Enemy_Location_System.add(sobj_sys)
          call lst_AI_Nearby_Enemy_Location_Sector.add(sobj_sect)
          call lst_AI_Nearby_Enemy_Location_Strength.add(sobj_strength)
          call lst_AI_Nearby_Enemy_Location_Assigned_Strength.add(0)
          call lst_AI_Nearby_Enemy_Location_Owner.add(sobj_owner)
          call lst_AI_Nearby_Enemy_Location_System_Distance.add(sys_dist)
          call lst_AI_Nearby_Enemy_Location_Ship_ID.add(sobj_id)
          call lst_AI_Nearby_Enemy_Location_Ship_Count.add(1)
          call lst_AI_Nearby_Enemy_Location_Strategic_Value.add(sobj_strategic_value)
          call lst_AI_Nearby_Enemy_Location_Ship_Min_Move.add(sobj_movement)
        endif

      // Location - Repair
      // List locations where we have ships repairing
      LOCATION_TYPE_REPAIR:
        set found_pos := 0
        set list_count := lst_AI_Repair_Location_System.count()

        if (list_count > 0) then
          for index := 1 to list_count do
            if (found_pos = 0) then
              if (lst_AI_Repair_Location_System.get(index) = sobj_sys) and (lst_AI_Repair_Location_Sector.get(index) = sobj_sect) then
                set found_pos := index
              endif
            endif
          endfor
        endif

        if (found_pos > 0) then
          call lst_AI_Repair_Location_Repair_Tonnage.set(found_pos, lst_AI_Repair_Location_Repair_Tonnage.get(found_pos) + sobj_strength)
        else
          call lst_AI_Repair_Location_System.add(sobj_sys)
          call lst_AI_Repair_Location_Sector.add(sobj_sect)
          call lst_AI_Repair_Location_Repair_Tonnage.add(sobj_strength)
        endif
    endcase
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Remove_Location
//------------------------------------------------------------------------
function Remove_Location returns boolean
params
  location_type:             long
  index:                     long
vars
  location_removed:          boolean := FALSE
  loc_sys_name:              string
  loc_sys_sect:              string
begin

  // Remove the specified location
  case (location_type)
    LOCATION_TYPE_DEFENSE:
      if (lst_AI_Defense_Location_System.get(index) > 0) then
        // Debug output
        set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Defense_Location_System.get(index))
        set loc_sys_sect := Convert_Sector_To_String(lst_AI_Defense_Location_Sector.get(index))
        call Sys_Debug_Print("Lists", "      - Removed Defense Location " + loc_sys_name + " at " + loc_sys_sect)
        // Delete the defense location
        call lst_AI_Defense_Location_System.delete(index)
        call lst_AI_Defense_Location_Sector.delete(index)
        call lst_AI_Defense_Location_Strength.delete(index)
        call lst_AI_Defense_Location_Assigned_Strength.delete(index)
        call lst_AI_Defense_Location_Owner.delete(index)
        call lst_AI_Defense_Location_System_Distance.delete(index)
        call lst_AI_Defense_Location_Ship_ID.delete(index)
        call lst_AI_Defense_Location_Ship_Count.delete(index)
        call lst_AI_Defense_Location_Ship_Min_Move.delete(index)
        set location_removed := TRUE
      endif
    LOCATION_TYPE_ATTACK:
      if (lst_AI_Attack_Location_System.get(index) > 0) then
        // Debug output
        set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Attack_Location_System.get(index))
        set loc_sys_sect := Convert_Sector_To_String(lst_AI_Attack_Location_Sector.get(index))
        call Sys_Debug_Print("Lists", "      - Removed Attack Location " + loc_sys_name + " at " + loc_sys_sect)
        // Delete the attack location
        call lst_AI_Attack_Location_System.delete(index)
        call lst_AI_Attack_Location_Sector.delete(index)
        call lst_AI_Attack_Location_Strength.delete(index)
        call lst_AI_Attack_Location_Assigned_Strength.delete(index)
        call lst_AI_Attack_Location_Owner.delete(index)
        call lst_AI_Attack_Location_System_Distance.delete(index)
        call lst_AI_Attack_Location_Ship_ID.delete(index)
        call lst_AI_Attack_Location_Ship_Count.delete(index)
        set location_removed := TRUE
      endif
    LOCATION_TYPE_NEARBY_ENEMY:
      if (lst_AI_Nearby_Enemy_Location_System.get(index) > 1) then
        // Debug output
        set loc_sys_name := Sys_Get_Solar_System_Name(lst_AI_Nearby_Enemy_Location_System.get(index))
        set loc_sys_sect := Convert_Sector_To_String(lst_AI_Nearby_Enemy_Location_Sector.get(index))
        call Sys_Debug_Print("Lists", "      - Removed Nearby Enemy Location " + loc_sys_name + " at " + loc_sys_sect)
        // Delete the nearby enemy location
        call lst_AI_Nearby_Enemy_Location_System.delete(index)
        call lst_AI_Nearby_Enemy_Location_Sector.delete(index)
        call lst_AI_Nearby_Enemy_Location_Strength.delete(index)
        call lst_AI_Nearby_Enemy_Location_Assigned_Strength.delete(index)
        call lst_AI_Nearby_Enemy_Location_Owner.delete(index)
        call lst_AI_Nearby_Enemy_Location_System_Distance.delete(index)
        call lst_AI_Nearby_Enemy_Location_Ship_ID.delete(index)
        call lst_AI_Nearby_Enemy_Location_Ship_Count.delete(index)
        call lst_AI_Nearby_Enemy_Location_Ship_Min_Move.delete(index)
        set location_removed := TRUE
      endif
  endcase

  return location_removed
end

//------------------------------------------------------------------------